PDA

View Full Version : Trapping action from an 'SDI' app



blaylockr
8th July 2008, 20:41
I have found post from users having similar problems displaying a form from another form/class when they used the designer. It seems easier to do this straight from code like the textEditor demo. However, I have designed an app in the designer as well as an about box and I would like to use them.

The problem is that since I can't change the header created by the designer, how do I create a custom slot that the button click action will call without modifying the generated header file?:confused:

wysota
8th July 2008, 22:06
Did your read the docs on using Designer generated forms in your application?

blaylockr
9th July 2008, 00:47
Yes, but I did not find an example that shows how to get around this problem. Do you have a particular example in mind? The best way I can find around this problem so far is to base my app off of the text editor demo app.

wysota
9th July 2008, 01:10
Honestly I don't see the problem :) Could you elaborate what _is_ the problem? Did you implement a widget class for each of the Designer generated components? What did you do next? What problem did you face?

blaylockr
9th July 2008, 18:21
Sorry, I had a hard time putting this problem into words.

I figured it out on my own... finaly. I was not aware that one could create one's own protection level keywords and when I was looking at the help files I didn't notice the

public slots: keyword.

I created a canonical version of the solution which I will now post for posterity:o
...
:confused: Errrr... Well, I can't seem to attach the file.

I guess I'll just try to summarize what I did here.

I created a "Main Window" form in the Qt Designer and changed it's object name to "QT_DesignerTestWindow". I added a menu item called "Launch some dialog box". The designer automatically created the action "actionLaunch_some_dialog_box".

I also created a Dialog and changed its object name to "SomeDialog".

I used qmake to create the .h files.

I created a main.cpp shown below.

I created a class derived from QMainWindow called MyMainWindow and added a function to that class in the 'private slots' protection level scope to act as my custom 'slot' (event callback function) also shown below.

Here is the code:



// main.cpp
#include <QApplication>

// include the Designer derived header
#include "ui_QT_designer_test.h"

// include the QMainWindow subclass header
#include "MyMainWindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MyMainWindow* mainWin = new MyMainWindow();

Ui::QT_DesignerTestWindow designerTestWindow;
designerTestWindow.setupUi(mainWin);

// attempt to setup the slot to show the dialog box
QObject::connect(designerTestWindow.actionLaunch_s ome_dialog_box,
SIGNAL(triggered()),
mainWin, SLOT( showDialog() ));

mainWin->show();
return app.exec();
}





// MyMainWindow.h
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H

#include <QMainWindow.h>

class MyMainWindow : public QMainWindow
{
Q_OBJECT
public:
MyMainWindow();
private slots:
void showDialog();
};
#endif




// MyMainWindow.cpp
#include "mymainwindow.h"
#include "ui_SomeDialogBox.h"

MyMainWindow::MyMainWindow( )
: QMainWindow()
{

}
void MyMainWindow::showDialog()
{
QDialog* showMe = new QDialog();
Ui::SomeDialog dialog;
dialog.setupUi(showMe);
showMe->show();
}

wysota
9th July 2008, 20:27
So more or less you did this:
http://doc.trolltech.com/latest/designer-using-a-component.html