Results 1 to 6 of 6

Thread: Problem about using Designer to create menu,add action and slot?

  1. #1
    Join Date
    Jan 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem about using Designer to create menu,add action and slot?

    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.
      

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Problem about using Designer to create menu,add action and slot?

    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:

    Qt Code:
    1. QWidget* w = new QWidget(this)
    To copy to clipboard, switch view to plain text mode 

    See: We even got syntax-highlighting and linking to the Qt-docs working !!!
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Feb 2010
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem about using Designer to create menu,add action and slot?

    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
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "FrmMain.h"
    3.  
    4. int main(int argc, char *argv[]) {
    5. QApplication app(argc, argv);
    6. FrmMain form;
    7. form.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    ui_FrmMain.h (autogenerated (and unaltered) from designer's .ui file)
    Qt Code:
    1. #ifndef UI_FRMMAIN_H
    2. #define UI_FRMMAIN_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QHeaderView>
    9. #include <QtGui/QMainWindow>
    10. #include <QtGui/QMenu>
    11. #include <QtGui/QMenuBar>
    12. #include <QtGui/QPushButton>
    13. #include <QtGui/QStatusBar>
    14. #include <QtGui/QWidget>
    15.  
    16. QT_BEGIN_NAMESPACE
    17.  
    18. class Ui_FrmMain
    19. {
    20. public:
    21. QAction *actionE_xit;
    22. QWidget *centralwidget;
    23. QPushButton *pushButton;
    24. QMenuBar *menubar;
    25. QMenu *menu_File;
    26. QStatusBar *statusbar;
    27.  
    28. void setupUi(QMainWindow *FrmMain)
    29. {
    30. if (FrmMain->objectName().isEmpty())
    31. FrmMain->setObjectName(QString::fromUtf8("FrmMain"));
    32. FrmMain->resize(164, 125);
    33. actionE_xit = new QAction(FrmMain);
    34. actionE_xit->setObjectName(QString::fromUtf8("actionE_xit"));
    35. centralwidget = new QWidget(FrmMain);
    36. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    37. pushButton = new QPushButton(centralwidget);
    38. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    39. pushButton->setGeometry(QRect(10, 20, 92, 28));
    40. FrmMain->setCentralWidget(centralwidget);
    41. menubar = new QMenuBar(FrmMain);
    42. menubar->setObjectName(QString::fromUtf8("menubar"));
    43. menubar->setGeometry(QRect(0, 0, 164, 25));
    44. menu_File = new QMenu(menubar);
    45. menu_File->setObjectName(QString::fromUtf8("menu_File"));
    46. FrmMain->setMenuBar(menubar);
    47. statusbar = new QStatusBar(FrmMain);
    48. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    49. FrmMain->setStatusBar(statusbar);
    50.  
    51. menubar->addAction(menu_File->menuAction());
    52. menu_File->addAction(actionE_xit);
    53.  
    54. retranslateUi(FrmMain);
    55. QObject::connect(pushButton, SIGNAL(clicked()), FrmMain, SLOT(PushButton_clicked()));
    56. QObject::connect(actionE_xit, SIGNAL(triggered()), FrmMain, SLOT(PushButton_clicked()));
    57.  
    58. QMetaObject::connectSlotsByName(FrmMain);
    59. } // setupUi
    60.  
    61. void retranslateUi(QMainWindow *FrmMain)
    62. {
    63. FrmMain->setWindowTitle(QApplication::translate("FrmMain", "FrmMain", 0, QApplication::UnicodeUTF8));
    64. actionE_xit->setText(QApplication::translate("FrmMain", "E&xit", 0, QApplication::UnicodeUTF8));
    65. pushButton->setText(QApplication::translate("FrmMain", "PushButton", 0, QApplication::UnicodeUTF8));
    66. menu_File->setTitle(QApplication::translate("FrmMain", "&File", 0, QApplication::UnicodeUTF8));
    67. } // retranslateUi
    68.  
    69. };
    70.  
    71. namespace Ui {
    72. class FrmMain: public Ui_FrmMain {};
    73. } // namespace Ui
    74.  
    75. QT_END_NAMESPACE
    76.  
    77. #endif // UI_FRMMAIN_H
    To copy to clipboard, switch view to plain text mode 

    FrmMain.h
    Qt Code:
    1. #ifndef _FRMMAIN_H
    2. #define _FRMMAIN_H
    3.  
    4. #include "ui_FrmMain.h"
    5.  
    6. class FrmMain : public QMainWindow {
    7. Q_OBJECT
    8. public:
    9. FrmMain();
    10. virtual ~FrmMain();
    11. private:
    12. Ui::FrmMain widget;
    13.  
    14. private slots:
    15. void PushButton_clicked();
    16. };
    17.  
    18. #endif /* _FRMMAIN_H */
    To copy to clipboard, switch view to plain text mode 


    FrmMain.cpp
    Qt Code:
    1. FrmMain::FrmMain() {
    2. widget.setupUi(this);
    3. }
    4.  
    5. FrmMain::~FrmMain() {
    6. }
    7.  
    8. void FrmMain::PushButton_clicked() {
    9. this->close();
    10. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem about using Designer to create menu,add action and slot?

    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?

  5. #5
    Join Date
    Feb 2010
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem about using Designer to create menu,add action and slot?

    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.

  6. #6
    Join Date
    Feb 2010
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem about using Designer to create menu,add action and slot?

    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).

    FYI:

    $ make help

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

Similar Threads

  1. Menu open action -reg
    By jsmith in forum Qt Programming
    Replies: 1
    Last Post: 8th May 2009, 11:07
  2. action -> slot in Qt Designer
    By yuriry in forum Qt Tools
    Replies: 4
    Last Post: 6th October 2008, 19:17
  3. Menu problem using Designer
    By JimBrown in forum Qt Tools
    Replies: 1
    Last Post: 19th February 2007, 21:47
  4. How to create custom slot in Qt Designer 4.1?
    By jamadagni in forum Qt Tools
    Replies: 31
    Last Post: 18th January 2006, 20:46
  5. Action on menu to display ui
    By jochen_r in forum Newbie
    Replies: 10
    Last Post: 10th January 2006, 10:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.