PDA

View Full Version : signals/slot functions not automatically generated in the .cpp or .h file?



joebats72
10th April 2011, 20:45
I have an application generated with QT wizard, using QMainwindow as the main class. In the .ui file, I created a menu with File, Open, etc. I’ve also created a pushbutton that clears the text of a Rich Text Edit control that’s on the form(and actually works!). I did this with the Signal and slot mechanism in QT designer, but I don’t see the functions for this in the code. I’m a bit confused as to what to do from here. Questions:

1)Do signal and slot functions get automatically generated in the code or do they have to be manually added?

2)How would I start to create additional dialog boxes to be attached to the menus? For example, if I want to invoke an open file dialog from File->Open, how would I do this?

3)How can I add additional dialogs to my menus using QT designer?

Please help, thx
joe

ChrisW67
10th April 2011, 22:47
1)Do signal and slot functions get automatically generated in the code or do they have to be manually added?

You have connected a signal that already exists (clicked()) in the code of QPushButton to a slot that already exists in the code of QTextEdit (clear()). You don't have to create these routines. Designer has generated the code to connect the two pre-existing methods. You can see that code in a file ui_mainwindow.h (a name matching your source file) that is generated when you build your project.

You have to declare any new signal that you wish to create for your own QObject derivatives, and let moc generate the code for that signal.
You have to declare and implement any new slot that you wish to create for your own QObject derivatives.
Open Qt Assistant and read about Signals and Slots (http://doc.qt.nokia.com/latest/signalsandslots.html)


2)How would I start to create additional dialog boxes to be attached to the menus? For example, if I want to invoke an open file dialog from File->Open, how would I do this?

Open Qt Assistant and read the Standard Dialogs (http://doc.qt.nokia.com/latest/dialogs-standarddialogs.html) and the Application (http://doc.qt.nokia.com/latest/mainwindows-application.html) example. These examples cover everything you have asked, and there are plenty of others.


3)How can I add additional dialogs to my menus using QT designer?

Qt Designer designs a single UI element at a time. You design the new dialog using Designer and write code to create and display it from other places.

joebats72
14th April 2011, 03:30
Hi, thanks for your input but I'm still having trouble here. I have a new dialog that I am trying to call from MainWindow. I included the "ui_dialog.h" in the MainWindow.cpp file. I have a menu with File, Test, Exit. I am trying to call the new dialog from "Test" but it just doesn't work. I am not sure how to display it. These functions are not found with the intellisense, but appear in the ui_dialog.h file. please help...I'm thinking something like this should work but doesnt:

void MainWindow::actionTest()
{
Dialog dlg;
dlg.show();

}:(

Lesiok
14th April 2011, 07:36
Dialog is showed and immediatly closed because out of scope. Try something like this :


void MainWindow::actionTest()
{
Dialog *dlg = new Dialog();
//to auto delete after close
dlg->setAttribute( Qt::WA_DeleteOnClose, true );
dlg->show();
}

ChrisW67
15th April 2011, 00:53
Hi, thanks for your input but I'm still having trouble here. I have a new dialog that I am trying to call from MainWindow. I included the "ui_dialog.h" in the MainWindow.cpp file.
There should be a class Dialog defined in a header (usually dialog.h) that you include in the MainWindow.cpp file. The machine generated ui_dialog.h file should remain private to the Dialog class implementation (usually dialog.cpp).


I have a menu with File, Test, Exit. I am trying to call the new dialog from "Test" but it just doesn't work. I am not sure how to display it. These functions are not found with the intellisense, but appear in the ui_dialog.h file. please help...I'm thinking something like this should work but doesnt:

void MainWindow::actionTest()
{
Dialog dlg;
dlg.show();

}
As pointed out above by Lesiok, the dlg instance of Dialog goes out of scope immediately after you show() the non-modal dialog because show() return immediately. If the dialog is meant to be non-modal then you need to allocate dlg on the heap as Lesiok says. If the dialog was meant to be modal then you should use QDialog::exec() rather than QWidget::show().

joebats72
18th April 2011, 00:48
Thanks again Chris & Losiok, but at this point I'm at a loss. I've attached the project, if someone can please check to see what the issue is. I've tried the options aforementioned and now the code builds and runs, but the dialog is not displayed when I select "Test" from the menu. Can you please help, thx joe

ChrisW67
18th April 2011, 02:34
Where have you connected the menu action triggered() signal to the MainWindow::actionTest() slot you want to execute?

joebats72
18th April 2011, 03:36
Hi, and thanks for your quick response. I actually did that now in QT designer, built the code but it still doesn't work. Not sure if I got this right, but here's a snapshot of what I did in Qt designer in my new version.

ChrisW67
18th April 2011, 06:02
You connected the action's triggered() signal to the show() slot of the main window (with no visible effect since the main window is already visible if the user can activate the action). Why do expect that MainWindow::show() will run the code in the actionTest() slot?

Try doing this connection in the MainWindow constructor without using Designer. You will probably learn more about what is going on.

You can do this in Designer but you have to manually tell Designer about the custom slots for MainWindow so that they appear in the relevant slot editor drop-down.

joebats72
19th April 2011, 16:09
Hi, thanks for pointing out my error. I am trying to connect the menu item (actionTest) to invoke my dialog. The “receiver” of the “signal” should be the dialog, and I think the slot should be “show()”. As you can see, I do not see the dialog object to “receive” the signal in Qt designer. Also, I tried to code this as you stated but it doesn’t work, so most likely I’m doing something wrong. What am I missing here? Can you provide a code snippet to assist here please? See code below and Qt designer snapshot attached.
Thanks again,
Joe



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "testdialog.h" //dialog header
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//TestDialog dlg;
// dlg.show(); //this works, but the dialog is invoked before main app
// dlg.exec();

// connect(actionTest(), SIGNAL(triggered(), this, SLOT(show());
//connect(MainWindow::show(),SIGNAL(triggered()),thi s, SLOT(show())));
connect(actionTest(), SIGNAL(triggered()), TestDialog::show(), SLOT(show());
}


MainWindow::~MainWindow()
{
delete ui;
}

ChrisW67
20th April 2011, 01:04
No, the receiver of the signal should a be a slot that creates and shows the dialog: you had one called actionTest() in your main window class. The dialog object doesn't exist until you create it.

Your connect() calls are broken and should be generating a compiler error or runtime warning. The arguments to the connect() are:

A pointer to the sender QObject
the SIGNAL() being sent
A pointer to the receiving object
the SLOT() receiving the signals when sent


So, with the slot in your main window class renamed doStuff() to highlight the distinction from the actionTest QAction:


connect(ui->actionTest, SIGNAL(triggered()), this, SLOT(doStuff()));

is closer to the mark.

joebats72
20th April 2011, 21:53
It works! I think I have a better handle on this now that I see how it works. Thanks very much for your help.

Joe