PDA

View Full Version : Connection problem with menubar using Designer



nikau
30th March 2009, 14:20
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:


connect( aboutAction, SIGNAL(triggered()), this, SLOT(about()) );

...seem to connect the menu-entry with the function

Thanks in advance :

Nikau


mainwindow.cpp

#include <QApplication>
#include <QStatusBar>
#include <QMessageBox>
#include "mainwindow.h"

Mainwindow::Mainwindow( QWidget *parent ) : QMainWindow( parent )
{

ui.setupUi( this );
createActions();
statusBar()->showMessage( tr("Done") );

}

void Mainwindow::about()//works
{
QMessageBox::about( this, tr("About Mainwindow"),
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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_Main.h"

class QAction;
class QTextEdit;
class QMenu;

class Mainwindow : public QMainWindow
{
Q_OBJECT

public:
Mainwindow( QWidget *parent = 0 );

protected:

private slots:
void createActions();
void about();


private:

QAction *aboutAction;
Ui::MainWindow ui;
};

#endif // MAINWINDOW_H


main.cpp

#include <QApplication>

#include "Mainwindow.h"

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

(new Mainwindow)->show();

return app.exec();
}

calhal
30th March 2009, 14:29
You've declared aboutAction inside your UI with QtDesigner, so you should refere to it like this:

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.

nikau
30th March 2009, 17:53
You've declared aboutAction inside your UI with QtDesigner, so you should refere to it like this:

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


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

calhal
30th March 2009, 19:25
Ups, I was writing fast from my memory ;)

I think the answer is in QtAssistant:


bool QObject::connect ( const QObject * sender, const char * signal,
const QObject * receiver, const char * method,
Qt::ConnectionType type = Qt::AutoConnection ) [static]