Results 1 to 5 of 5

Thread: Open a window with a QAction in QToolBar

  1. #1
    Join Date
    Jun 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Open a window with a QAction in QToolBar

    Hi,

    I have a mainwindow with a toolbar. When the user clicks to a tool named DialogBox, I want my QWidget ''dialogue'' to open, so that the user can fill the questions ask in it. So, I tried to link the click signal of that tool to a slot (open_dialog()) where the coding of QWidget 'dialogue'' is,including a dialogue.show().
    But that doesn't work...how can I do this ?

    Qt Code:
    1. mainwindow::mainwindow(QWidget*p)
    2. {
    3.  
    4. QWidget *central= new QWidget(0);
    5. ........
    6. ...........
    7. .........
    8. central->setLayout(layout);
    9.  
    10. setCentralWidget(central);
    11.  
    12.  
    13. // Tool bar creation
    14. QToolBar *toolBarFichier = addToolBar("Fichier");
    15.  
    16. QAction *actionQuitter = toolBarFichier->addAction("&Quit");
    17. toolBarFichier->addAction(actionQuitter);
    18. actionQuitter->setShortcut(QKeySequence("Ctrl+Q"));
    19. actionQuitter->setIcon(QIcon("quit.png"));
    20. connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));
    21.  
    22. //Informations about patient
    23. QAction *actionDialogue = toolBarFichier->addAction("&Dialog box");
    24. toolBarFichier->addAction(actionDialogue);
    25. actionDialogue->setShortcut(QKeySequence("Ctrl+Q"));
    26. actionDialogue->setIcon(QIcon("file.png"));
    27. QObject::connect(actionDialogue, SIGNAL(triggered()), qApp, SLOT(open_dialog()));
    28.  
    29.  
    30. }
    31.  
    32. void mainwindow::open_dialog()
    33. {
    34. QWidget dialogue;
    35.  
    36. QPalette pal = dialogue.palette();
    37. pal.setColor(dialogue.backgroundRole(),Qt::white);
    38. dialogue.setPalette(pal);
    39.  
    40. QLabel *intro = new QLabel("Informations relatives à la patiente");
    41. QFont *font1;
    42.  
    43. QLabel *nameLabel = new QLabel("Nom:");
    44. QLineEdit *nameLineEdit = new QLineEdit;
    45.  
    46. QLabel *prenomLabel = new QLabel("Prénom:");
    47. QLineEdit *prenomLineEdit = new QLineEdit;
    48.  
    49. QLabel *ageLabel = new QLabel("Âge:");
    50. QLineEdit *ageLineEdit = new QLineEdit;
    51.  
    52. QLabel *poidsLabel = new QLabel("Poids (kg):");
    53. QLineEdit *poidsLineEdit = new QLineEdit;
    54.  
    55. QLabel *nbr_accLabel = new QLabel("Nombre d'accouchements vécus :");
    56. QLineEdit *nbr_accLineEdit = new QLineEdit;
    57.  
    58.  
    59. QPushButton *bouton1 = new QPushButton("Enregistrer ");
    60. QPushButton *bouton2 = new QPushButton("Terminer");
    61.  
    62. QGridLayout *layout = new QGridLayout;
    63. layout->addWidget(intro,0, 0);
    64. layout->addWidget(nameLabel, 2, 0);
    65. layout->addWidget(nameLineEdit, 2, 2,1,3);
    66. layout->addWidget(prenomLabel, 3, 0);
    67. layout->addWidget(prenomLineEdit, 3,2, 1,3);
    68. layout->addWidget(ageLabel, 4, 0);
    69. layout->addWidget(ageLineEdit, 4, 2);
    70. layout->addWidget(poidsLabel, 5, 0);
    71. layout->addWidget(poidsLineEdit, 5, 2);
    72. layout->addWidget(nbr_accLabel, 6, 0);
    73. layout->addWidget(nbr_accLineEdit, 6, 2);
    74. layout->addWidget(bouton1, 8, 3);
    75. layout->addWidget(bouton2, 8, 4);
    76.  
    77. dialogue.setLayout(layout);
    78.  
    79.  
    80. dialogue.show();
    81.  
    82. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your helps !

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Open a window with a QAction in QToolBar

    It is a slot of your mainwindow and not of the application! Use
    Qt Code:
    1. QObject::connect(actionDialogue, SIGNAL(triggered()), this, SLOT(open_dialog()));
    To copy to clipboard, switch view to plain text mode 
    Also make sure you have declared open_dialog as a slot.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Open a window with a QAction in QToolBar

    Aren't you get some errors that the slot doesn't exist?

    I think is because of this line:
    Qt Code:
    1. //QObject::connect(actionDialogue, SIGNAL(triggered()), qApp, SLOT(open_dialog()));
    2. //should be:
    3. QObject::connect(actionDialogue, SIGNAL(triggered()), this, SLOT(open_dialog()));
    4. //as i see from your code the open_dialog slot is member of MainWindow
    To copy to clipboard, switch view to plain text mode 
    And also:
    Qt Code:
    1. void mainwindow::open_dialog()
    2. {
    3. //QWidget dialogue; //you might want to allocate dynamically and pass this pointer as a parent
    4. //something like this:
    5. QWidget *dialogue = new QWidget(this);
    6. //or even use the QDialog class: QDialog *dialogue = new QDialog(this);
    7. //...
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open a window with a QAction in QToolBar

    oops right!!
    thanks !!

    I still have a little problem ....on my QDialog widget (I used the QDialog class instead of just QWidget, as proposed by Zlatomir)
    I have a button to close it with the connection of the quit slot, but when I use it, my mainwindow closes at the same time, and I only want the QDialog widget to close...

    Qt Code:
    1. void mainwindow::ouvrir_dialogue()
    2. {
    3. QDialog *dialogue = new QDialog(this);
    4. dialogue->setModal(true);
    5. QPalette pal = dialogue->palette();
    6. pal.setColor(dialogue->backgroundRole(),Qt::white);
    7. dialogue->setPalette(pal);
    8.  
    9. .......................
    10. .........................
    11.  
    12. QPushButton *bouton1 = new QPushButton("Enregistrer ");
    13. QPushButton *bouton2 = new QPushButton("Terminer");
    14.  
    15. ...........................
    16.  
    17. dialogue->show();
    18.  
    19. QObject::connect(bouton1, SIGNAL(clicked()), this, SLOT(save()));
    20. QObject::connect(bouton2, SIGNAL(clicked()), qApp, SLOT(quit()));
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Open a window with a QAction in QToolBar

    Try like this:
    Qt Code:
    1. QObject::connect(bouton2, SIGNAL(clicked()), dialogue, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 

    LE: You need to connect the Sender (Object), sender Signal, receiver (object) here don't just use the application object, and receiver Slot()

Similar Threads

  1. Open a window inside another window
    By passerb in forum Qt Programming
    Replies: 6
    Last Post: 18th October 2009, 13:24
  2. how to add QAction vertically and horizontally in QToolBar
    By sanjayshelke in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2008, 09:56
  3. QToolBar and QAction
    By !Ci in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2008, 12:41
  4. QToolbar in Window Vista prob.
    By patrick772goh in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 10:35
  5. right way to open a new window
    By wind in forum Newbie
    Replies: 1
    Last Post: 1st November 2006, 11:17

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
  •  
Qt is a trademark of The Qt Company.