PDA

View Full Version : connect doesnt work



john_god
20th November 2008, 03:16
:confused:I have a very simple application, a main window with a toolbar with two buttons.
I want to click a button and make a dialog or a messagebox appear, but nothing happens when I click.
What am I doing wrong?


This is my code

main.cpp


#include "matematica.h"

#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Matematica w;
w.show();
return a.exec();
}


matematica.h


#ifndef MATEMATICA_H
#define MATEMATICA_H

#include <QtGui/QMainWindow>
#include "ui_matematica.h"
#include <QMessageBox>
#include "Grafico2D_Dlg.h"


class Matematica : public QMainWindow
{
Q_OBJECT

public:
Matematica(QWidget *parent = 0);
~Matematica();

private:
Ui::MatematicaClass ui;

private slots:
void newFile();
void Graf2D();

private:
Grafico2D_Dlg *gr2D;

private:
void createActions();

QAction *newAction;
QAction *graf2DAction;



};

#endif // MATEMATICA_H


matematica.cpp


#include "matematica.h"

Matematica::Matematica(QWidget *parent)
: QMainWindow(parent)
{
createActions();
ui.setupUi(this);

}

Matematica::~Matematica()
{

}

void Matematica::createActions()
{

newAction = new QAction(tr("&New"), this);
newAction->setIcon(QIcon(":/images/new.png"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new file"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));



/*graf2DAction = new QAction(tr("&Gráfico 2D"), this);
newAction->setIcon(QIcon(":/images/graf2d.png"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new graph 2D"));
connect(graf2DAction, SIGNAL(triggered()), this, SLOT(Graf2D()));
*/

graf2DAction = new QAction(QIcon(":/images/graf2d.png"),tr("Grafico 2D"),this);
graf2DAction->setStatusTip(tr("Create a new graph 2D"));
connect(graf2DAction, SIGNAL(triggered()),this, SLOT(Graf2D()));


}


void Matematica::newFile()
{

QMessageBox::about(this, tr("New File"),
tr("<h2>New File</h2>"));
}

void Matematica::Graf2D()
{
QMessageBox::about(this, tr("Grafico 2D"),
tr("<h2>Grafico 2D</h2>"));
}

QbelcorT
20th November 2008, 06:19
Hi,
I have not used QAction before but I am using other signals and slots.
Just a few tips:
1) if debugging, you should see in the debugger if the slots/signals connected ok, when initializing. If there is a problem it will tell you 'cannot connect signal/slot' etc.
2) in the slots of your widget try inserting 'qDebug() << "Test" ; or anything similar. See if the slot is being executed. void Matematica::newFile(), void Matematica::Graf2D(). If it's being executed then there is something wrong with your messagebox implementation.

Maybe you are missing this. Put this in your void Matematica::createActions() area.
addAction(graf2DAction);
addAction(newAction);

john_god
21st November 2008, 13:29
I added the AddAction but still doesnt work.

I havent tried the debug because I dont know yet how to debug
I have made the connection work in some buttons I added to the spreadsheet tutorial, but in this projct that I am starting from scratch, with the eclipse and qt designer, it doenst work.

wysota
21st November 2008, 17:25
The action has to be used somewhere (menu, button) for its shortcut to work.

john_god
23rd November 2008, 09:36
In the constructor I added


ui.toolBar->addAction(graf2DAction);


This creates a empty (iconless) button in the toolbar that make connect work, but the existente button still doesnt work.


Another question, the QAction "graf2DAction" was created outside the QtDesigner (in the class Matematica). But I have a QAction created in the QDesigner "actionGr_ficos_2D", so why not use it in connect like this:


connect(ui.actionGr_ficos_2D, SIGNAL(triggered()),this, SLOT(Graf2D()));
but the problem is that this blows up the aplicattion

So wich action to use? The one created in Designer, or write one hand coded? How to associated with the toolbar button ?