[英]How to make Qt Mainwindow attached to each other?
I created two QMainWindow class ui in Qt. 我在Qt中创建了两个QMainWindow类ui。 MainWindowB is shown after clicking a button in MainWindowA. 单击MainWindowA中的按钮后,将显示MainWindowB。 I want to attach MainWindowB to MainWindowA (when the two mainwindows are moved close, they snap together like magnets), so that they can be moved together as "attached". 我想将MainWindowB附加到MainWindowA(当两个主窗口关闭时,它们像磁铁一样卡在一起),以便可以将它们作为“已附加”一起移动。 If they are dragged apart, they will be just regular QMainWindow. 如果将它们分开,它们将只是常规的QMainWindow。 How can I make that happen? 我该如何实现?
I tried to use QDockWidget but when it "attached" (docked), it affects other components in the original MainWindowA. 我尝试使用QDockWidget,但是当它“附加”(对接)时,它会影响原始MainWindowA中的其他组件。
mainwindowA.cpp mainwindowA.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
MainWindowB *newWin= new MainWindowB;
newWin->show();
}
mainwindowB.cpp mainwindowB.cpp
#include "mainwindowb.h"
#include "ui_mainwindowb.h"
MainWindowB::MainWindowB(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindowB)
{
ui->setupUi(this);
}
MainWindowB::~MainWindowB()
{
delete ui;
}
You can implement this pretty straightforward: 您可以实现以下非常简单的方法:
#include <QApplication>
#include <QMainWindow>
class CMasterWindow : public QMainWindow
{
Q_OBJECT
public:
CMasterWindow(QWidget *parent = Q_NULLPTR)
: QMainWindow(parent)
{
setWindowTitle(tr("master"));
}
~CMasterWindow() { }
virtual void moveEvent(QMoveEvent *me) Q_DECL_OVERRIDE
{
QMainWindow::moveEvent(me);
emit SignalMoved(QRect(pos(), size()));
}
virtual void resizeEvent(QResizeEvent *re) Q_DECL_OVERRIDE
{
QMainWindow::resizeEvent(re);
emit SignalMoved(QRect(pos(), size()));
}
signals:
void SignalMoved(QRect const &r);
};
class CSlaveWindow : public QMainWindow
{
Q_OBJECT
public:
CSlaveWindow(CMasterWindow *master)
: QMainWindow(master),
mpMaster(master),
mbLocked(master != Q_NULLPTR)
{
setWindowTitle(tr("slave"));
if(mpMaster != Q_NULLPTR)
{
bool ok = connect(mpMaster, &CMasterWindow::SignalMoved, this, &CSlaveWindow::MasterMovedSlot);
Q_ASSERT(ok);
mMasterRect = QRect(mpMaster->pos(), mpMaster->size());
MasterMovedSlot(mMasterRect);
}
}
~CSlaveWindow() { }
static int const snapMargin = 16;
virtual void moveEvent(QMoveEvent *me) Q_DECL_OVERRIDE
{
QMainWindow::moveEvent(me);
if(pos() == mExpectedMove)
{
// ignore self-initiated move (from MasterMovedSlot())
return;
}
mbLocked = QRect(mMasterRect.topRight(), 2 * QSize(snapMargin, snapMargin)).intersects(QRect(pos(), QSize(snapMargin, snapMargin)));
MasterMovedSlot(mMasterRect);
}
private slots:
void MasterMovedSlot(QRect const &r)
{
if(mbLocked)
{
mExpectedMove = r.topRight() + QPoint(snapMargin, 0);
move(mExpectedMove);
}
mMasterRect = r;
}
private:
CMasterWindow *mpMaster;
bool mbLocked;
QPoint mExpectedMove;
QRect mMasterRect;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CMasterWindow *master = new CMasterWindow();
master->show();
(new CSlaveWindow(master))->show();
return a.exec();
}
You need to define one window as master and the other one as slave - else you won't be able to tell the difference between the user wanting to move both windows together or wanting to tear one window from the other. 您需要将一个窗口定义为主窗口,将另一个窗口定义为从窗口-否则,您将无法分辨出希望将两个窗口一起移动或希望将一个窗口与另一个窗口撕裂的用户之间的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.