Accessing a Dialog toolButtons from the MainWindow
-
Hello all,
I have several toolButtons on a subwindow ui class called "homeview" which basically are shortcuts to some actions on the main menu. These toollButtons are used to open some other subwindows controlled in the MDI of my application. I would like to connect their click events to some slots in the mainwindow class called "mainWin". I have included homeview.h in the mainwin.h header and tried creating a pointer to homeview (...homeview *hm...). Compilation fails; below is one of my connections:
@ connect(hm->ui->stdBtn, SIGNAL(triggered()),
this, SLOT(nstud())); @
I get the following error:
......mainwin.cpp:38: error: invalid use of incomplete type 'class Ui::homeview'
connect(hm->ui->stdBtn, SIGNAL(triggered()),Could someone give a hint.
-
Ok wild guess here: I don't think QToolbuttons or QPushbuttons for that matter have a triggered signal. I think the signals are clicked,pressed,released and toggle; these are all inherited from QAbstractButton.
JT
-
Thx thecyclist,
Actually I corrected that before your reply but still I get the same compilation error. How can i really access the clicked signal of stdBtn from ui class "homeview" and connect it to my slot in "mainwin". I will be grateful for your assistance
-
I am not sure yet but I have done something very similar. In my case I had a modeless dialog showing info to the user that allowed the user to continue to use the main window. As the dialog was modeless I could not process the accept signal as normal, which would have been:
if( dlg.exec() == QDialog::Accepted)
{
// Do stuff...
}so I used a 'connect' in my mainwindow:
connect( m_pDlg , SIGNAL(accepted()) , this ,SLOT(ProcessDialog()));
where m_pDlg is a pointer to my dialog class. I also 'disconnected' the signal/slot connection when the dialog was closed by the user.
Having said all this the problem my be an #include probem; the error message is referring to an 'incomplete type' so the solution could involve making sure the definition [.h] for your 'homeview' class is included in your 'mainwin' .h.
JT
-
..and of course you have already checked this looking back at your original post...
JT
-
Yes, homeview.h is already included in the mainwin header...thx for your response. I am still trying to find a way. My scenario is a bit different. I have googled the solution to this with no vail.
I will be grateful for any contribution further -
You can add a signal to your dialog and not try to access the dialog controls directly (probably why it fails compilation).
// declarations in MyDialog signals: void Button1_Clicked(void); void Button2_Clicked(void); // constructor of MyDialog connect(btn1,SIGNAL(clicked(void)),this,SIGNAL(Button1_Clicked(void))); connect(btn2,SIGNAL(clicked(void)),this,SIGNAL(Button2_Clicked(void))); // main window connect(dialog,SIGNAL(Button1_Clicked(void)),this,SLOT(Option1(void))); connect(dialog,SIGNAL(Button2_Clicked(void)),this,SLOT(Option2(void)));
-
@Rondog
Thanks for your reply. That really compiled but when i click the toolButton I tested on, my application crushed. How do you code the "void Button1_Clicked(void);" signal. I suspect mine coding could be wrong:@
void homeview::btnClicked(void)
{
emit ui->stdBtn->clicked();
}
@ -
Thanks @Rondog, it has worked. I din't have to redefine the signal since its automatically generated by moc. Am so grateful!
Now I have many toolButtons on the home page, I would love to use the same signal for all buttons. Can you please help with the right loop to use...I was thinking a *switch *or if...else if loop can be implemented in this case but I can't figure the opening statement as shown below:
switch (/*some statement*/) case 1: connect(ui->stdBtn, SIGNAL(clicked()),this,SIGNAL(btnClicked())); break; case 2: connect(ui->tchBtn, SIGNAL(clicked()),this,SIGNAL(btnClicked())); break; I will be grateful
-
There is probably several ways you could do this. I assume in your main window you probably have slots to handle all of the 'click' events from the QToolButtons (maybe QAction ?) which means your test loop should probably be in your main window.
In your dialog you can use QSignalMapper to combine all of your button click events into a single slot and identify which button was clicked and emit this to the main window.
In your main window you would look at the button identification from the signal and call the appropriate action slot.
I guess it all depends on how many buttons you are dealing with. If you only have a half dozen or so it may not be worth the effort.
// example from: http://www.qtcentre.org/archive/index.php/t-27436.html MyDialog::MyDialog(...) { ui->setupUi(this); //QPushButton *buttonx[9]; buttonx[0] = ui->btn1; buttonx[1] = ui->btn2; buttonx[2] = ui->btn3; buttonx[3] = ui->btn4; buttonx[4] = ui->btn5; buttonx[5] = ui->btn6; buttonx[6] = ui->btn7; buttonx[7] = ui->btn8; buttonx[8] = ui->btn9; QSignalMapper *signalMapper = new QSignalMapper(this); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(myButtonPressed(int))); for(int i = 0; i < 9; i++) { signalMapper->setMapping(buttonx[i], i + 1); // this sets the 'ID' connect(buttonx[i], SIGNAL(clicked(void)), signalMapper, SLOT(map(void))); } } void MyDialog::myButtonPressed(int id) { emit Button_Clicked(id); // assumes you have signal 'void Button_Clicked(int);' } // Main Window // assumes you have slot 'void Dialog_Button_Clicked(int);' MainWindow::MainWindow(...) { connect(dialog,SIGNAL(Button_Clicked(int)),this,SLOT(Dialog_Button_Clicked(int))); } void MainWindow::Dialog_Button_Clicked(int id) { switch(id) { case 1: // call slot representing button number 1 break; case 2: // call slot representing button number 2 break; case 3: // call slot representing button number 3 break; } }