关键字:QSplitter、QGridLayout 1、界面设计中经常需要左边是树结构,点击树的节点在右边窗口显示内容,左边和右边的分界线是可以左右移动的,这时就需要用到QSplitter控件,在MainWindow.ui设计中,拖入控件QTreeWidget和一个QWidget控件,QWidget控件为右窗口; 2、同时选中这两个控件,点击右键,选择“Lay out”,再选择“Lay out Horizontally in splitter”; 3、右键点击MainWindow,选择“Lay out”,再选择“Lay out in Grid”; 4、在MainWindow.cpp中添加如下代码: ui->splitter->setStretchFactor(1,7); 5、下面我们来实现右边窗口的四分屏,首先右键点击QWidget控件,选择“Lay out”,再选择“Lay out in Grid”; 6、在工程中添加四个界面类Form1、Form2、Form3、Form4,这时工程就增加了form1.h、form1.cpp,form2.h、form2.cpp,form3.h、form3.cpp,form4.h、form4.cpp; 6、在工程中添加一个类Canvas,继承自QWidget,这时工程就多了一个canvas.h和canvas.cpp,这个类用来实现右边窗口的四分屏; 7、canvas.cpp的代码如下: #include "canvas.h"
Canvas::Canvas(QWidget *parent) : QWidget(parent) { layout = new QGridLayout;
widget1 = new Form1; widget1->setObjectName("widget1"); widget2 = new Form2; widget2->setObjectName("widget2"); widget3 = new Form3; widget3->setObjectName("widget3"); widget4 = new Form4; widget4->setObjectName("widget4");
vertsplitter1 = new QSplitter; vertsplitter2 = new QSplitter;
hozrsplitter = new QSplitter;
layoutup = new QGridLayout; layoutdown = new QGridLayout;
vertsplitter1->setOrientation(Qt::Vertical); vertsplitter2->setOrientation(Qt::Vertical);
hozrsplitter->setOrientation(Qt::Horizontal);
vertsplitter1->addWidget(widget1); vertsplitter1->addWidget(widget3);
vertsplitter2->addWidget(widget2); vertsplitter2->addWidget(widget4);
hozrsplitter->addWidget(vertsplitter1); hozrsplitter->addWidget(vertsplitter2);
layout->addWidget(hozrsplitter); layout->setMargin(0); layout->setSpacing(1);
widget1->setFocusPolicy(Qt::ClickFocus); widget2->setFocusPolicy(Qt::ClickFocus); widget3->setFocusPolicy(Qt::ClickFocus); widget4->setFocusPolicy(Qt::ClickFocus);
setLayout(layout); widget1->installEventFilter(this); widget2->installEventFilter(this); widget3->installEventFilter(this); widget4->installEventFilter(this);
vertsplitter1->setStretchFactor(0,1); vertsplitter1->setStretchFactor(1,1);
vertsplitter2->setStretchFactor(0,1); vertsplitter2->setStretchFactor(1,1);
hozrsplitter->setStretchFactor(0,1); hozrsplitter->setStretchFactor(1,1);
vertsplitter1->setHandleWidth(1); vertsplitter2->setHandleWidth(1);
hozrsplitter->setHandleWidth(1);
} Canvas::~Canvas() {
}
8、Form1类中的代码如下: #include "form1.h" #include "ui_form1.h"
Form1::Form1(QWidget *parent) : QWidget(parent), ui(new Ui::Form1) { ui->setupUi(this);
}
Form1::~Form1() { delete ui; }
void Form1::focusInEvent(QFocusEvent *event) { Q_UNUSED(event); repaint(); }
void Form1::focusOutEvent(QFocusEvent *event) { Q_UNUSED(event); repaint(); }
void Form1::paintEvent(QPaintEvent *event) { Q_UNUSED(event); if(this->isFullScreen()) { QPixmap pic; QPainter painter(this); painter.drawPixmap(rect(),pic); painter.end(); }
if(this->hasFocus()) { QPainter painter(this); painter.setPen(QPen(QColor(255, 255, 255), 1, Qt::SolidLine)); painter.drawRect(0,0,this->rect().width() - 1,this->rect().height() - 1); painter.end(); } else { QPainter painter(this); painter.setPen(QPen(QColor(0, 240,255), 1, Qt::SolidLine)); painter.drawRect(0,0,this->rect().width() - 1,this->rect().height() - 1); painter.end(); } }
void Form1::dragEnterEvent(QDragEnterEvent *event) { if(event->mimeData()->hasFormat("application/itemdata")) { event->setDropAction(Qt::MoveAction); event->accept(); }
if(this->layout()->itemAt(0) != 0) { currentitem = this->layout()->itemAt(0); this->layout()->itemAt(0)->widget()->hide(); } qDebug("Drag Enter"); }
void Form1::dragMoveEvent(QDragMoveEvent *event) { if(event->mimeData()->hasFormat("application/itemdata")) { event->setDropAction(Qt::MoveAction); event->accept(); } }
void Form1::dragLeaveEvent(QDragLeaveEvent *event) { Q_UNUSED(event); if(currentitem != 0) { currentitem->widget()->show(); currentitem = 0; } qDebug("DragLeave"); }
void Form1::dropEvent(QDropEvent *event) { qDebug("Drop IN"); if(event->mimeData()->hasFormat("application/itemdata")) { QByteArray itemdata = event->mimeData()->data("application/itemdata"); QDataStream dataStream (&itemdata,QIODevice::ReadOnly); QString dragitemname; dataStream>>dragitemname;
emit draginitem(dragitemname);
event->setDropAction(Qt::MoveAction); event->accept(); } }
9、Form2、Form3、Form4类中代码类似与步骤8中代码; 10、在MainWindow.cpp中增加如下代码: mCanvas = new Canvas; ui->widgetLayout->addWidget(mCanvas); 把四分屏窗体加载到了MainWindow的右边窗口; 11、在MainWindow界面,点击右键选择“Add Tool Bar”; 12、Action Editor窗口中增加两个Action,给它们分别取名“单屏”和“四分屏”,把这两个Action拖到Tool Bar上,在MainWindow.cpp中增加如下代码: void MainWindow::on_action_triggered() { mCanvas->widget1->show(); mCanvas->widget2->hide(); mCanvas->widget3->hide(); mCanvas->widget4->hide(); }
void MainWindow::on_action4_triggered() { mCanvas->widget1->show(); mCanvas->widget2->show(); mCanvas->widget3->show(); mCanvas->widget4->show(); } 实现点击“单屏”时,界面右边窗口显示一个屏,点击“四分屏”时,界面右边窗口显示四分屏;
需要观看运行效果或需要完整源代码,请到www.toutiao.com/i6845282122186359310/链接评论区写上你的邮箱