PDA

View Full Version : Absolute beginner in need of help



imperator
6th November 2009, 16:28
Hi

I'm new to these forums and new to the world of Qt.

Currently I'm working on a small assignment that asks me to create a window containing a label and a push button. When you click the button a new window should be generated, containing a line edit element (for the user to write something) and a push button. When you hit the button in this window, the window should close and the label in the first window should be updated with the text written in the line edit element (The second window is more or like a copy of QInputDialog). I would like to add that I do not use any designer for this project.

I'm able to build the windows as required, but I am not sure how to generate/call the second window from the first one.

I've named my classes MainWindow (containing a label and a push button) and EditWindow (containing line edit and push button). Both classes inherits from QDialog.

In MainWindow I use the push button clicked as a signal and I've named the slot openNewWindow:


connect(newWindowButton, SIGNAL(clicked()), this, SLOT(openNewWindow()));

The slot:

void MainWindow::openNewWindow(){
//QString txt = QInputDialog::getText(this, tr("Write text"), tr("Write some text:"));

//returnLabel->setText(txt);

// Some code to open a new window (EditWindow)
}

As you can see I've tested the slot using QInputDialog and applying the text returned to returnLabel (the naming of the variables might be a bit bad).

What I want to do in the slot is to open a the second window (EditWindow), but I'm not sure what the correct syntax for this would be to do this. I'm comfortable with one window, but not two or more.

This is probably simple, but not being very confident in my own abilities in Qt at the moment, I would greatly appreciate some tips or suggestions.

I hope this post is understandable, as English is not my first language.

Regards,

André

qtUser500
6th November 2009, 17:28
Not a problem, that's what these forums are for. :)

To call the second window you need to first instantiate the calling window and then execute it. It might look like this:



void MainWindow::openNewWindow()
{
EditWindow myEditWindow(); // call the constructor of the second window
myEditWindow.exec(); // This executes the window, and it will pop up

if (myEditWindow.result == QDialog::Accepted)
{
// Write your code here to retrieve what ever you want from the second window.
// In the Second window you will need to create a slot for accept(), code below
}

//You can then close or hide the second window
myEditWindow.close();

}


In the Second window write the slot for accept() and link it to the 'clicked' signal (This depends on the buttons you have on your ui to close the second window Eg OK | Cancel or is it more buttons Eg: Save | Save All | Cancel).
Using the accept() helps to link it to the appropriate button

EditWindow.h file



slot
accept(); // For OK or Save
reject(); // For cancel


EditWindow.cpp file


connect (ui.OKButton, SIGNAL(clicked()), this, SLOT(accept()));

void EditWindow::accept()
{
QDialog::accept();
//And whatever other code you might want to put here Eg validate before closing the window etc

}


Read the documents (Qt Assistant) for QDialog() and you will see the other slots there Eg open(), exec() etc

Good luck! :D

imperator
6th November 2009, 18:26
That was just what I've been looking for! Thank you very much!

I will read the documentation for QDialog as I'm not completely sure about what's actually happening here.

Thanks again :)

Regards,

André

qtUser500
6th November 2009, 20:31
U r welcome :)
A good place to start is reading the documentation.
Search for 'An Overview of Qt's Examples' in QT Assistant and U will get an idea of how things work.

TeresaML
24th November 2009, 10:38
I'm doing something similar but I'm lost in some aspects... Could you put the code of the EditWindow Class. Thanks!

imperator
24th November 2009, 11:02
Here goes:



EditWindow::EditWindow(QWidget* parent): QDialog(parent){

// Layout as you prefer it

connect(sendButton, SIGNAL(clicked()), this, SLOT(accept()));

}

void EditWindow::accept(){
QDialog::accept();
}

QString EditWindow::getText(){
return textEdit->text();
}

TeresaML
24th November 2009, 13:21
I've tried to do this but I have an error with the code line "myEditWindow.exec();":
Error 1 error C2228: the left operate of '.exec' must have class/struct/union.

Any idea please?

imperator
24th November 2009, 13:40
I'm a newbie in Qt, so if anyone more qualified than me could answer, that would be nice.

But the error message say that the left operate of myEditWindow.exec() must be of a class, etc. How did you declare myEditWindow?

I did it this way from my MainWindow class:


EditWindow ew(this);
ew.exec();

TeresaML
24th November 2009, 14:11
Thank you very much! The mistake was here. I forgot to put "this". I'm a newbie in Qt too, and I'm plenty of doubts and troubles...

Now, I will try to put some buttons and other widgets in the second window. I hope that I don't disturb so much with my questions. Thanks a lot!