3

我创建了 2 个类,每个类:

  1. 作为QWidget父母
  2. Q_OBJECT
  3. 初始化一些动作,创建菜单栏和工具栏,并将动作连接到它们

然后我创建了主程序文件,通过指针创建QMainWindow并初始化了我的两个类。qmainwnd

而我所拥有的 - 菜单有效,第二个工具栏有效,但第一个工具栏(由类 1 创建)不响应鼠标点击。

怎么回事?为什么我什至不能移动第一个工具栏或单击它的按钮?

这是一个示例:

主文件

#include <QApplication>
#include <QMainWindow>

#include "CModDocument.h"
#include "CModEditor.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow wnd;

    // init CA
    CModDocument a(&wnd);

    // init CB
    CModEditor b(&wnd);

    wnd.show();

    return app.exec();
}

CModDocument.h

#ifndef CMODDOCUMENT_H
#define CMODDOCUMENT_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QAction;
class QToolBar;
class QMainWindow;
QT_END_NAMESPACE

class CModDocument: public QWidget
{

    Q_OBJECT

public:
    CModDocument(QWidget *parent = 0);

    QMainWindow *getMainWnd();

public slots:
    void newFile();
    void open();
    bool save();
    bool saveAs();

private:
    void createActions();
    void createToolBars();
    void interCom();

    QMainWindow *mainWnd;

    QToolBar *fileToolBar;
    QAction *newAct;
    QAction *openAct;
    QAction *saveAct;
    QAction *saveAsAct;
};

#endif // CMODDOCUMENT_H

CModDocument.cpp

#include <QtGui>
#include <QDebug>
#include "CModDocument.h"

CModDocument::CModDocument( QWidget *parent )
: QWidget( parent )
, mainWnd( (QMainWindow*)parent )
{
    createActions();
    createToolBars();
    interCom();

    mainWnd->statusBar()->showMessage(tr("Ready"));
}

void CModDocument::newFile()
{
    qDebug() << "newFile";
}

void CModDocument::open()
{
    qDebug() << "open";
}

bool CModDocument::save()
{
    qDebug() << "save";
    bool retVal;
    return retVal;
}

bool CModDocument::saveAs()
{
    qDebug() << "saveAs";
    bool retVal;
    return retVal;
}


void CModDocument::createActions()
{
    newAct = new QAction(tr("&New"), this);
    newAct->setShortcuts(QKeySequence::New);
    newAct->setStatusTip(tr("Create a new file"));

    openAct = new QAction(tr("&Open..."), this);
    openAct->setShortcuts(QKeySequence::Open);
    openAct->setStatusTip(tr("Open an existing file"));

    saveAct = new QAction(tr("&Save"), this);
    saveAct->setShortcuts(QKeySequence::Save);
    saveAct->setStatusTip(tr("Save the document to disk"));

    saveAsAct = new QAction(tr("Save &As..."), this);
    saveAsAct->setShortcuts(QKeySequence::SaveAs);
    saveAsAct->setStatusTip(tr("Save the document under a new name"));
}

void CModDocument::createToolBars()
{
    fileToolBar = mainWnd->addToolBar(tr("File"));
    fileToolBar->addAction(newAct);
    fileToolBar->addAction(openAct);
    fileToolBar->addAction(saveAct);
}

void CModDocument::interCom()
{
    connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
    connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
    connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
}

CModEditor.h

#ifndef CMODEDITOR_H
#define CMODEDITOR_H

#include <QWidget>
// #include "CModEdiWidget.h"

QT_BEGIN_NAMESPACE
class QMainWindow;
class QAction;
class QMenu;
class QMenuBar;
class QToolBar;
QT_END_NAMESPACE

class CModEditor : public QWidget
{
    Q_OBJECT

public:
    CModEditor(QWidget *parent = 0);

public slots:
    void cut();

private:
    void createActions();
    void createToolBars();
    void interCom();

    QAction *cutAct;
    QToolBar *editToolBar;

    // parent
    QMainWindow *mainWnd;
};

#endif

CModEditor.cpp

#include <QtGui>
#include <QDebug>

#include "CModEditor.h"

CModEditor::CModEditor(QWidget *parent)
: QWidget(parent)
, mainWnd( (QMainWindow*)parent )
{
    createActions();
    createToolBars();
    interCom();
}

void CModEditor::cut()

{
    qDebug() << "cut";
}
void CModEditor::createActions()
{
    cutAct = new QAction(tr("Cu&t"), this);
    cutAct->setShortcuts(QKeySequence::Cut);
    cutAct->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
}

void CModEditor::createToolBars()
{
    editToolBar = mainWnd->addToolBar(tr("Edit"));
    editToolBar->addAction(cutAct);
    editToolBar->setIconSize(QSize(16, 16));
}

void CModEditor::interCom()
{
    connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
}

构建.pro

CONFIG      -= app_bundle
HEADERS         = CModDocument.h   CModEditor.h
SOURCES         = CModDocument.cpp CModEditor.cpp main.cpp
4

1 回答 1

2

问题似乎是您正在尝试将 配置QMainWindow为两个QWidgets 的父级。main()我按如下方式修改了您的功能并且它起作用了:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow wnd;

    QWidget w;

    // init CA
    CModDocument a(&wnd, &w);

    // init CB
    CModEditor b(&wnd, &w);

    wnd.show();

    return app.exec();
}

注意新QWidget w的父母ab。我什至不确定我的方法是否足够(可能不是)。我认为最好添加一个QLayouttow和 add aand bto that QLayout。然后你可以设置wwnd'中心小部件。

于 2011-07-28T01:15:47.427 回答