PDA

View Full Version : Opening a seperate QMainWindow (Yes I've already looked at the FAQ)



bbad68
29th January 2010, 18:44
Hey all,

I'm creating a new form (let's call it 'A') that I want to open up another form ('B') that I recently completed. I KNOW that the FAQ talks about something similiar, but to my understanding, it doesnt address my particular problem. So essentially, I have 2 QMainWindows both in seperate directories, and since I'm new, I don't really know how to get one to access the other.

What I initially tried to do was to copy-paste B's .ui, .cpp, and .h files into A's directory, then included them in project A. The problem is that both A and B are QMainWindows (rather than having B as a QDialog as the FAQ implies), and so I get the following error when I try to compile the code:

error C2664: 'Ui_ISocGeneratorClass::setupUi' : cannot convert parameter 1 from 'ISocGenerator *const ' to 'QMainWindow *

Which occurs at the following line:


ui.setupUi(this);

So is there a simple way for me to convert B to a QDialog? What I tried to do was just change the QMainWindow to QDialog in the class constructor (ISocGenerator is what I've referred to as 'B'):

In the .h file


class ISocGenerator : public QDialog

In the class constructor in the .cpp file:


ISocGenerator::ISocGenerator(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)

So, judging from the error message, I think that I have to change my ISocGenerator to a QDialog in the actual .ui file, but I don't know how to do that. What can I do?

ChrisW67
29th January 2010, 21:08
If I understand you correctly, you want one application that either shows two different QMainWindows from start-up, or have one create the other when requested (dialog style). This is where I'd start:

Build main window A in Designer. Let's call it MyMainA.
Build main window B in Designer with a different layout or whatever. Let's call this class MyMainB.


Now you have two classes that, when instantiated, create a main window. If you want both to show from application start then:

#include <QApplication>
#include "mymaina.h"
#include "mymainb.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyMainA mainA;
MyMainB mainB;
mainA.show();
mainB.show();
return app.exec();
}
or if MyMainA should launch MyMainB then edit the MyMainA class code to respond to a button click (or whatever) and then


void MyMainA::on_aButton_clicked() {
MyMainB *mb = new MyMainB(this);
mb->show();
}


If you already have MyMainB built in another project (that's what I think you mean by both in seperate directories) you could copy its source files into the new project and add them to the project's pro file. (Duplicating code like this is not ideal but that's another story). If you simply stored MyMainB source elsewhere when you built it in the current project then Qt Creator should have put correct paths in the pro file, and it should just build.

bbad68
29th January 2010, 21:57
Yeah, myMainB was built in another project, sorry for the confusion. It's working now though, so thanks a lot :)