1 Attachment(s)
Connection problem with menubar using Designer
Hi,
This seems so simple but I haven't been able to figure it out yet.. All help will be appreciated :)
I have tried to create a Mainwindow (mainwidget) with menubar holding an entry "Help", and I have created an "aboutAction"-item and dragged it into the help menu using the designer! Saved as main.ui! (check attachment)
I have three other files: mainwindow.cpp, mainwindow.h and main.cpp (see below) and it seems to compile as it should but when I try to execute the About action nothing happens.
I have tested the about() function by just executing it on program start and it shows a QMessageBox as intended, Now why doesn't the line:
Code:
connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
...seem to connect the menu-entry with the function
Thanks in advance :
Nikau
mainwindow.cpp
Code:
#include <QApplication>
#include <QStatusBar>
#include <QMessageBox>
#include "mainwindow.h"
{
ui.setupUi( this );
createActions();
statusBar()->showMessage( tr("Done") );
}
void Mainwindow::about()//works
{
tr("<h2>Mainwindow v. 0.01</h2>\n"
"An application to help test various aspects of QT4\n"
"Thanks to anybody helping out :-)\n") );
}
void Mainwindow::createActions()
{
aboutAction
= new QAction(tr
("&About"),
this);
connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
}
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_Main.h"
{
Q_OBJECT
public:
protected:
private slots:
void createActions();
void about();
private:
Ui::MainWindow ui;
};
#endif // MAINWINDOW_H
main.cpp
Code:
#include <QApplication>
#include "Mainwindow.h"
int main( int argc, char **argv )
{
(new Mainwindow)->show();
return app.exec();
}
Re: Connection problem with menubar using Designer
You've declared aboutAction inside your UI with QtDesigner, so you should refere to it like this:
Code:
connect( &ui.aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
If you run your code from console I guess you'll get message that object aboutAction doesn't exist.
Re: Connection problem with menubar using Designer
Quote:
Originally Posted by
calhal
You've declared aboutAction inside your UI with QtDesigner, so you should refere to it like this:
Code:
connect( &ui.aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
If you run your code from console I guess you'll get message that object aboutAction doesn't exist.
Actually what seemed to work is the following
Code:
connect( ui.aboutAction, SIGNAL(triggered()), this, SLOT(about()) );
When would I use the &??
I don't recal having to call ui.action when doing this...
Well thank you for helping out :-)
Re: Connection problem with menubar using Designer
Ups, I was writing fast from my memory ;)
I think the answer is in QtAssistant:
Code:
const QObject * receiver,
const char * method,
Qt::ConnectionType type = Qt::AutoConnection ) [static]