PDA

View Full Version : Problem about using Designer to create menu,add action and slot?



sharkmouse
14th January 2010, 05:22
hello,
i am a newer of qt,and use designer,i found i can add some menu and sub menu of the mainwindow,can add a action to each menu item and a slot of the action,but i does not work,what is the matter?
  i use the vs 2008 with qt integration plugin.i created a new project of qt application. i add a menu item called 'SYSTEM,the objectname is :menuSYSTEM,then the plugin can create the code below:
  menuSYSTEM = new QMenu(menuBar);
menuSYSTEM->setObjectName(QString::fromUtf8("menuSYSTEM"));
  
after that ,i add a new action,and the Text property of the action is 'SYSTEM'。the code is:
  actionSysterm = new QAction(MainWindow);
actionSysterm->setObjectName(QString::fromUtf8("actionSysterm"));
menuBar->addAction(menuSYSTEM->menuAction());
  
now i add a slot which the sender is the action:
  QObject::connect(actionSystem,SIGNAL(triggered() ),MainWindow,SLOT(close());

after i compile the code,i does not work。so i tried to modify the code of 'ui_Mainwindow.h'
change menuBar->addAction(menuSYSTEM->menuAction());
to
menuBar->addAction(actionSystem);
now it works.
what is the matter?whether my procedure is right?
i think that modify the ui_xxx.h is not a normal ,is that right?who can tell me how to do it?
thanks.
  

axeljaeger
19th January 2010, 21:30
Right, it is not normal to modify the ui_file. Never ever do that.

If the action was shown in the first case, check whether connect returned true. It returns false if the connect failed.

See whether you got any runtime warning about a failed connection.

Ah: There is a nice feature in vBulletiin: Use [ code ] tags for snippets like this:


QWidget* w = new QWidget(this)

See: We even got syntax-highlighting and linking to the Qt-docs working !!!

NotANoob
3rd February 2010, 21:39
I'm experiencing the same problem as axeljaeger. I'm using designer to create a menu action and connect its triggered() signal to a defined slot in my app. It simply doesn't work, its as though connect() in my ui_file.h file isn't working ... but there is no complaining about failure to connect in the terminal window when app is run.

While waiting for my Qt Centre account to activate (so I could post same question) I tried building a release, rather than a debug load and that fixed(?) the problem ... the menu action now connects properly to the designated slot. I made another debug load, and the problem came back, another release load and the problem went away.

It seems there's a legitimate bug here somewhere, either in a Qt library, or in the totally automated build system (I'm using NetBeans 6.8 IDE to handle build).

Below is my code if anyone is interested ... incidentally, the pushButton connect (which is functionally very similiar to the actionE_xit action) works in both debug and release loads.

main.cpp


#include <QtGui/QApplication>
#include "FrmMain.h"

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
FrmMain form;
form.show();
return app.exec();
}


ui_FrmMain.h (autogenerated (and unaltered) from designer's .ui file)


#ifndef UI_FRMMAIN_H
#define UI_FRMMAIN_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_FrmMain
{
public:
QAction *actionE_xit;
QWidget *centralwidget;
QPushButton *pushButton;
QMenuBar *menubar;
QMenu *menu_File;
QStatusBar *statusbar;

void setupUi(QMainWindow *FrmMain)
{
if (FrmMain->objectName().isEmpty())
FrmMain->setObjectName(QString::fromUtf8("FrmMain"));
FrmMain->resize(164, 125);
actionE_xit = new QAction(FrmMain);
actionE_xit->setObjectName(QString::fromUtf8("actionE_xit"));
centralwidget = new QWidget(FrmMain);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
pushButton = new QPushButton(centralwidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(10, 20, 92, 28));
FrmMain->setCentralWidget(centralwidget);
menubar = new QMenuBar(FrmMain);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 164, 25));
menu_File = new QMenu(menubar);
menu_File->setObjectName(QString::fromUtf8("menu_File"));
FrmMain->setMenuBar(menubar);
statusbar = new QStatusBar(FrmMain);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
FrmMain->setStatusBar(statusbar);

menubar->addAction(menu_File->menuAction());
menu_File->addAction(actionE_xit);

retranslateUi(FrmMain);
QObject::connect(pushButton, SIGNAL(clicked()), FrmMain, SLOT(PushButton_clicked()));
QObject::connect(actionE_xit, SIGNAL(triggered()), FrmMain, SLOT(PushButton_clicked()));

QMetaObject::connectSlotsByName(FrmMain);
} // setupUi

void retranslateUi(QMainWindow *FrmMain)
{
FrmMain->setWindowTitle(QApplication::translate("FrmMain", "FrmMain", 0, QApplication::UnicodeUTF8));
actionE_xit->setText(QApplication::translate("FrmMain", "E&xit", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("FrmMain", "PushButton", 0, QApplication::UnicodeUTF8));
menu_File->setTitle(QApplication::translate("FrmMain", "&File", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class FrmMain: public Ui_FrmMain {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_FRMMAIN_H


FrmMain.h


#ifndef _FRMMAIN_H
#define _FRMMAIN_H

#include "ui_FrmMain.h"

class FrmMain : public QMainWindow {
Q_OBJECT
public:
FrmMain();
virtual ~FrmMain();
private:
Ui::FrmMain widget;

private slots:
void PushButton_clicked();
};

#endif /* _FRMMAIN_H */



FrmMain.cpp


FrmMain::FrmMain() {
widget.setupUi(this);
}

FrmMain::~FrmMain() {
}

void FrmMain::PushButton_clicked() {
this->close();
}

norobro
3rd February 2010, 22:51
Your code works fine on my machine (Qt 4.6.0; Debian Sid) debug or release mode.

I used a text editor and the command line. Have you tried running it from the command line?

NotANoob
4th February 2010, 13:39
Nope. Building and/or running from the command line makes no difference. Release version, all works. Debug version, menu action doesn't connect to slot. I'm using Qt 4.5.0, Ubuntu 9.04, gcc 4.3.3. I've no idea how to fix. I guess the work around for my current configuration is to not build in Debug mode.

NotANoob
4th February 2010, 17:15
OK, some progress ... sort of.

Having given up on this particular problem, I went on to start another Qt project. Used designer to set up a QMainWindow with a QVBoxLayout as its central widget, built a release load and it seg faulted somewhere in my ui_file.h file on a call to my QVBoxLayout object. Tried a debug load and it worked (just opposite of what was happening above). So I dug through the makefile system that NetBeans generates and found that there are make options available at the command prompt that are not available via the IDE. From the project directory:

$ make clobber

This clears out everything but the source and .ui files (.o, core, executables, etc.). Then, also from project directory:

$ make CONF=Debug build
$ make CONF=Release build

This builds a debug version, and a release version. After this both version of my new program worked. I tried this on the afore mentioned project ... also worked. Apparently something is not getting updated properly with a simple build. Whether its NetBean's make system, or the compiler itself, I do not know (although based on my experience so far with this version of gcc, l'm leaning toward the compiler:confused:).

FYI:

$ make help

from the project directory will describe the command line options available through NetBean's makefile system.