Results 1 to 5 of 5

Thread: connect doesnt work

  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default connect doesnt work

    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

    Qt Code:
    1. #include "matematica.h"
    2.  
    3. #include <QtGui>
    4. #include <QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. Matematica w;
    10. w.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 


    matematica.h

    Qt Code:
    1. #ifndef MATEMATICA_H
    2. #define MATEMATICA_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_matematica.h"
    6. #include <QMessageBox>
    7. #include "Grafico2D_Dlg.h"
    8.  
    9.  
    10. class Matematica : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. Matematica(QWidget *parent = 0);
    16. ~Matematica();
    17.  
    18. private:
    19. Ui::MatematicaClass ui;
    20.  
    21. private slots:
    22. void newFile();
    23. void Graf2D();
    24.  
    25. private:
    26. Grafico2D_Dlg *gr2D;
    27.  
    28. private:
    29. void createActions();
    30.  
    31. QAction *newAction;
    32. QAction *graf2DAction;
    33.  
    34.  
    35.  
    36. };
    37.  
    38. #endif // MATEMATICA_H
    To copy to clipboard, switch view to plain text mode 


    matematica.cpp

    Qt Code:
    1. #include "matematica.h"
    2.  
    3. Matematica::Matematica(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. createActions();
    7. ui.setupUi(this);
    8.  
    9. }
    10.  
    11. Matematica::~Matematica()
    12. {
    13.  
    14. }
    15.  
    16. void Matematica::createActions()
    17. {
    18.  
    19. newAction = new QAction(tr("&New"), this);
    20. newAction->setIcon(QIcon(":/images/new.png"));
    21. newAction->setShortcut(QKeySequence::New);
    22. newAction->setStatusTip(tr("Create a new file"));
    23. connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
    24.  
    25.  
    26.  
    27. /*graf2DAction = new QAction(tr("&Gráfico 2D"), this);
    28. newAction->setIcon(QIcon(":/images/graf2d.png"));
    29. newAction->setShortcut(QKeySequence::New);
    30. newAction->setStatusTip(tr("Create a new graph 2D"));
    31. connect(graf2DAction, SIGNAL(triggered()), this, SLOT(Graf2D()));
    32. */
    33.  
    34. graf2DAction = new QAction(QIcon(":/images/graf2d.png"),tr("Grafico 2D"),this);
    35. graf2DAction->setStatusTip(tr("Create a new graph 2D"));
    36. connect(graf2DAction, SIGNAL(triggered()),this, SLOT(Graf2D()));
    37.  
    38.  
    39. }
    40.  
    41.  
    42. void Matematica::newFile()
    43. {
    44.  
    45. QMessageBox::about(this, tr("New File"),
    46. tr("<h2>New File</h2>"));
    47. }
    48.  
    49. void Matematica::Graf2D()
    50. {
    51. QMessageBox::about(this, tr("Grafico 2D"),
    52. tr("<h2>Grafico 2D</h2>"));
    53. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: connect doesnt work

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

  3. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: connect doesnt work

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: connect doesnt work

    The action has to be used somewhere (menu, button) for its shortcut to work.

  5. #5
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question Re: connect doesnt work

    In the constructor I added

    Qt Code:
    1. ui.toolBar->addAction(graf2DAction);
    To copy to clipboard, switch view to plain text mode 


    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:

    Qt Code:
    1. connect(ui.actionGr_ficos_2D, SIGNAL(triggered()),this, SLOT(Graf2D()));
    To copy to clipboard, switch view to plain text mode 
    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 ?

Similar Threads

  1. connect problem QDialog
    By Zergi in forum Newbie
    Replies: 1
    Last Post: 1st January 2008, 13:36
  2. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  3. qobject connect questions
    By VireX in forum Newbie
    Replies: 5
    Last Post: 20th May 2007, 09:04
  4. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20
  5. [QT4 & XP] connect on QtreeView
    By incapacitant in forum Newbie
    Replies: 1
    Last Post: 2nd March 2006, 11:08

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.