试图完成一个可横屏竖屏切换的界面,考虑到横屏以后文字也须要转过来,因此使用QGraphicsView将widget加入QGraphicsScene中,旋转QGraphicsView使整个界面旋转。
代码:
main.cppgit
#include "Widget.h" #include <QApplication> #include <QGraphicsScene> #include <QObject> #include "MyGraphicsProxyWidget.h" #include "Manager.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Manager m; return a.exec(); }
Manager.cpp 初始化QGraphicsView和QGraphicsScene,将Widget加入QGraphicsScene,并处理旋转后窗口大小的调整github
#include "Manager.h" #include <QDebug> #include <QScrollBar> Manager::Manager(QObject *parent) : QObject(parent) { widget = new Widget; scene = new QGraphicsScene; scene->setSceneRect(widget->geometry()); w = scene->addWidget(widget); myView = new MyGraphicsProxyWidget(w); //w->setRotation(180); view = new QGraphicsView(scene); view->setWindowFlags(Qt::FramelessWindowHint);//无边框 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //view->resize(1000,1000); //view->setTransformationAnchor(QGraphicsView::NoAnchor); QObject::connect(widget,SIGNAL(changeRotate(int)),this,SLOT(changeRotate(int))); QObject::connect(widget,SIGNAL(changeRotate(int)),myView,SLOT(changeRotate(int))); view->setFrameStyle(QFrame::NoFrame);//无Frame边框 view->show(); } void Manager::changeRotate(int) { static int rotateCount = 1; if(rotateCount ==1 || rotateCount == 3){ view->resize(400,300); }else{ view->resize(300,400); } if(rotateCount == 4) rotateCount = 0; ++rotateCount; }
MyGraphicsProxyWidget.cpp 处理旋转QGraphicsView,并移动view位置web
#include "MyGraphicsProxyWidget.h" #include <QDebug> MyGraphicsProxyWidget::MyGraphicsProxyWidget(QGraphicsProxyWidget * view,QObject *parent) : QObject(parent) { this->view = view; } void MyGraphicsProxyWidget::changeRotate(int rotate) { qDebug()<<view->size()<<view->pos()<<view->mapToParent(0,0)<<view->mapToScene(0,0); view->setRotation(rotate); static int rotateCount = 1; if(rotateCount == 1){ view->moveBy(350,0); }else if(rotateCount == 2){ view->moveBy(-50,400); }else if(rotateCount == 3){ view->moveBy(-350,-100); }else{ view->moveBy(50,-300); } if(rotateCount == 4) rotateCount = 0; ++rotateCount; }
Widget.cpp 触发旋转less
#include "Widget.h" #include "ui_Widget.h" #include <QPainter> #include <QTransform> #include <QMessageBox> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_clicked() { static int rotate = 0; rotate += 90; if(rotate >= 360) rotate = 0; emit changeRotate(rotate); } void Widget::on_checkBox_clicked() { QMessageBox::information(this,"ssssssssss","xccccccc"); }
效果:
简化一下,界面添加布局:
Manager.cpp 去掉changeRotate中窗口大小的调整svg
#include "Manager.h" #include <QDebug> #include <QScrollBar> Manager::Manager(QObject *parent) : QObject(parent) { widget = new Widget; scene = new QGraphicsScene; scene->setSceneRect(widget->geometry()); w = scene->addWidget(widget); myView = new MyGraphicsProxyWidget(w); //w->setRotation(180); view = new QGraphicsView(scene); view->setWindowFlags(Qt::FramelessWindowHint);//无边框 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //view->resize(1000,1000); //view->setTransformationAnchor(QGraphicsView::NoAnchor); QObject::connect(widget,SIGNAL(changeRotate(int)),this,SLOT(changeRotate(int))); QObject::connect(widget,SIGNAL(changeRotate(int)),myView,SLOT(changeRotate(int))); view->setFrameStyle(QFrame::NoFrame);//无Frame边框 view->show(); } void Manager::changeRotate(int) { //static int rotateCount = 1; //if(rotateCount ==1 || rotateCount == 3){ // view->resize(400,300); //}else{ // view->resize(300,400); //} //if(rotateCount == 4) // rotateCount = 0; //++rotateCount; }
MyGraphicsProxyWidget.cpp changeRotate中加上view->resize,这样界面就能自适应屏幕:布局
#include "MyGraphicsProxyWidget.h" #include <QDebug> MyGraphicsProxyWidget::MyGraphicsProxyWidget(QGraphicsProxyWidget * view,QObject *parent) : QObject(parent) { this->view = view; } void MyGraphicsProxyWidget::changeRotate(int rotate) { qDebug()<<view->size()<<view->pos()<<view->mapToParent(0,0)<<view->mapToScene(0,0); view->setRotation(rotate); static int rotateCount = 1; if(rotateCount ==1 || rotateCount == 3){ view->resize(400,300); }else{ view->resize(300,400); } if(rotateCount == 1){ view->moveBy(300,0); }else if(rotateCount == 2){ view->moveBy(0,400); }else if(rotateCount == 3){ view->moveBy(-300,0); }else{ view->moveBy(0,-400); } if(rotateCount == 4) rotateCount = 0; ++rotateCount; }
效果:就和手机横屏竖屏效果差很少了。。。代码是附件二
ui
代码:https://github.com/yangyang0312/QtTestCode/tree/master/testViewRotatethis