PDA

View Full Version : how to use menuBar and Action



sax94
10th February 2012, 18:19
hy,
how I just said yesterday I'm trying to build my first GUI application. I want my interface to stay very simple with just one button that starts the countdown and then changes into stop to stop the countdown. I wanted to have at the top in the menuBar something called File and if you click on it another menu appears and there is the voice set starting value. until here no problem, but I want to click on set string value and see another window appear, how can I do this? I created another Qt Designer form class, but maybe a Qt Designer form would be enough?

KillGabio
10th February 2012, 19:02
Well, it seems that you dont have you ideas very clear. Research about signals and slots (connect), there`s a lot of information over the internet. The idea is that when you trigger an action of your menu something happens, for example a Dialog shows up, that means that the signal trigger () of the action calls the slot showAdialog. Then you may want to return to the view of the mainWindow, so you need to figure out a way to indicate the mainWindow to show, either by finishing the execution of the Dialog or a signal from the dialog telling the mainWindow it has finished.

sax94
11th February 2012, 11:20
hy,
I saw on the nokia developer site that to connect a signal to a slot you should use connect(sender, SIGNAL(signal), receiver, SLOT(slot));
in my case this sould be


QObject::connect(ui->actionModifica_valore_iniziale,SIGNAL(triggered()) ,dialog,SLOT(show()));

so my menuFile Modifica_valore_iniziale that stands for set value, emits the signall triggered(). in the receiver spot I must put what I want to see wuen the signal is emitted, but if I put dialog Qt tells me that dialog wasn't declared, but if I put ui.menuBar and then I click the action the menuBar appears, same if I put ui.menuFile, the menuFile stays there. so what do I have to do?
thank's for the help

KillGabio
11th February 2012, 17:54
MMM i think you are connecting the objects the wrong way...You have a mainWindow with a menuBar and you want a dialog to pop-up...right?

So the right thing to connect is this (in the constructor of mainWindow):



connect(ui->actionModifica_valore_iniziale,SIGNAL(triggered()) ,this, SLOT(//this slot shows the dialog));


then mainWindow has a SLOT called
//this slot shows the dialog

that will do something like this:



void //this slot shows the dialog{
myDialog *dialog = new myDialog ();
//do something for example:
dialog->setData (ui->labelText->text ()); //you assign data from a label of the mainWindow to the dialog...
dialog->exec();
}


it is important that you understand the concept because this is used like a lot...

hope it helps!

PS:- with exec () you assure that the focus is going to return to mainWindow when the dialog closes

sax94
13th February 2012, 10:39
hy,
thank's for yout help, I managed to open the dialog, now In my dialog there is a spinbox, I want that when he closes the dialog, the value in the spinbox to appear in the label in the main window. i write this in the dialog constructor


QObject::connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(DisplayData ()));

but in my slot DisplayData() I can't set the label value, this is my code


void MainWindow::DisplayData()
{
MainWindow *c;
c->setwhat?
//........
}

how can I do it?

KillGabio
13th February 2012, 13:12
In that case you have to emit your own signal...you haven`t read the documentation at all right? lol....in your case it would be something like this:

First you need to connect the accept button from your dialog with a slot of the dialog class, and also create a signal in that class


signals:
void dialog_accepted (QString)

the slot from the dialog needs to be connected to a click event when you create the class (do that)


void slot_when_button_clicked (){
//mmm
//yo do staff
// --here comes the important part
emit dialog_accepted (ui->spinbox->currentText()); //look the function of spinbox that returns current value
close ();
}


then in your main window you connect the dialog signal "dialog_accepted" with a display slot of the mainwin...something like (in mainwindow)



connect (dialog, SIGNAL (dialog_accepted (QString)), this, SLOT(display_spin_from_dialog (QString)));


have a look at signals and slots because if not you are going to come here soon asking for more :P

hope it helps!