开发者

QT hangs my toolbar and its buttons

开发者 https://www.devze.com 2023-03-19 08:41 出处:网络
I\'ve created 2 classes, each: has QWidget as a parent has Q_OBJECT macros inits some actions, creates menubar and toolbar, and connects actions to them

I've created 2 classes, each:

  1. has QWidget as a parent
  2. has Q_OBJECT macros
  3. inits some actions, creates menubar and toolbar, and connects actions to them

Then I created the main program file, created QMainWindow, and init my two classes by qmainwnd pointer.

And what I've got - menu works, the second toolbar works, but the first toolbar (created by class 1) doesn't respond to mouse clicks.

What happed with it? Why I can not even move this first toolbar or click its buttons?

Here a sample:

main.cpp

#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()));
}

build.pro

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


The problem seems to be that you are trying to configure the QMainWindow as the parent to two QWidgets. I modified your main() function as follows and it worked:

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();
}

Notice the new QWidget w that parents a and b. I'm not even sure my approach is adequate (it probably isn't). I think it is better to add a QLayout to w and add a and b to that QLayout. Then you could set w as wnd's central widget.

0

精彩评论

暂无评论...
验证码 换一张
取 消