如何使Qt Mainwindow相互连接? [英] How to make Qt Mainwindow attached to each other?

查看:84
本文介绍了如何使Qt Mainwindow相互连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Qt 中创建了两个 QMainWindow 类 ui.单击 MainWindowA 中的按钮后显示 MainWindowB.我想将 MainWindowB 附加到 MainWindowA (当两个主窗口移近时,它们像磁铁一样吸附在一起),以便它们可以连接"在一起移动.如果将它们分开,它们将只是普通的 QMainWindow.我怎样才能做到这一点?

I created two QMainWindow class ui in Qt. MainWindowB is shown after clicking a button in MainWindowA. 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". If they are dragged apart, they will be just regular QMainWindow. How can I make that happen?

我尝试使用 QDockWidget,但是当它附加"(停靠)时,它会影响原始 MainWindowA 中的其他组件.

I tried to use QDockWidget but when it "attached" (docked), it affects other components in the original 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.

这篇关于如何使Qt Mainwindow相互连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆