PDA

View Full Version : Basic GUI Help



QT++
18th August 2012, 09:58
Hello everyone,

To start off I have 3 questions and they mainly relate to GUI/ Qt coding. This is my 1st GUI program and I am converting my c++ console application
to a GUI after putting it off for over a year.

1. I have setup a process to be run when a PushButton is clicked which contains lots of if statements. I would like to know how to stop the program from executing that action any further once it hits a certain point. For example in my console program I would have something like


if (1 == 1){
// do stuff
}

if(1 == 2){
// do stuff
}

if(1 == 3)
{
// do stuff
return 0;
}


How would I do the same with the GUI?

2. I am using a Plain Text Edit box to display information output from my program. Currently I am using something like



ui->textBox->SetPlainText("words here");


But then if I wish to write to the same text box later on in my program it overwrites all text in the TextBox. How do I make it just additional information to the text box rather than overwrite everything. Note the program is writing to the box and not me.

3. Lastly I have my program setup over multiple .cpp files and have functions I wish to call from other source files which isnt a problem, that's normal. But I would normally use cout << "text here" << endl; within the functions in another cpp file (another than my main). I am using the same code above to try and put some text to screen but getting an error. I asume it has something to do with ui not being declared within the secondary .cpp file I have stored my functions in. Note the following code is within the function from the secondary cpp file.



ui->textBox->SetPlainText("words here");


Any help on these would be greatly appreciated. I am sure they are basic issues I have overlooked.

Zlatomir
18th August 2012, 16:37
2) You might want to use append (http://qt-project.org/doc/qt-4.8/qtextedit.html#append) instead of the second SetPlainText() (or maybe insert, look in the documentation link)
3) If i understand correctly you need a slot or member function in the class that contains the ui, and call that function with the text you need to set/append/insert... and in that function use setText/append...
1) i didn't understand your question, are you trying to code something like: cin >> var; if(var == 1); ? If so in gui application you can have a button that once it is clicked it performs the operations, and/or use some textEdit to take input.

QT++
18th August 2012, 23:04
2) You might want to use append (http://qt-project.org/doc/qt-4.8/qtextedit.html#append) instead of the second SetPlainText() (or maybe insert, look in the documentation link)
3) If i understand correctly you need a slot or member function in the class that contains the ui, and call that function with the text you need to set/append/insert... and in that function use setText/append...
1) i didn't understand your question, are you trying to code something like: cin >> var; if(var == 1); ? If so in gui application you can have a button that once it is clicked it performs the operations, and/or use some textEdit to take input.

Thanks for the reply, append was the function I was looking for, just didnt know it's name.

1. I am actually trying to create a situation where the code will stop executing the function (slot) that is running if certain conditions are met. To do this in console I would just return 0. What is the GUI equivalent?

3. Not quite, I have the slot function website but I am also calling my own functions within that function. To make the program easier to maintain and keep track of I have stored some of those functions in separate .cpp files. I am now running into UI issues when calling those functions from another .cpp file and executing them within main. The functions within the secondary .cpp file write to ui and I think this is the issue. Here is something which I am looking to do (and the header files are all linked correctly etc).

mainwindow.cpp



void MainWindow::on_processButton_clicked(){
// do stuff
randomFunction();
}


second.cpp



randomFunction(){
// do stuff
ui->randomBox->setPlainText("text here");
}

Zlatomir
19th August 2012, 02:45
1. I am actually trying to create a situation where the code will stop executing the function (slot) that is running if certain conditions are met. To do this in console I would just return 0. What is the GUI equivalent?

There is no "gui" alternative for that, just use return; (you can't use return 0; because the slot/function returns usually returns void not int).

...3. Not quite, I have the slot function website but I am also calling my own functions within that function. To make the program easier to maintain and keep track of I have stored some of those functions in separate .cpp files. I am now running into UI issues when calling those functions from another .cpp file and executing them within main. The functions within the secondary .cpp file write to ui and I think this is the issue. Here is something which I am looking to do (and the header files are all linked correctly etc).

You need a pointer to the MainWindow class and a public slot/function to call (this public function you will access ui and call setText or append) to update the ui, a simple alternative is:


void MainWindow::on_processButton_clicked(){
// do stuff
randomFunction(this); //pass the pointer to instance... you can use signals and slots for a more flexible connection
}

void MainWindow::UpdateUi(QString updatedText)
{
ui->randomBox->setPlainText(updatedText); //use this function to update the ui, in mainwindow.h declare it as public: so that you can call in from outside of MainWindow objects
}

//second.cpp
randomFunction(MainWindow* mainWindowInstance){
// do stuff
mainWindowInstance->UpdateUi("text here"); //call the update function with the help of the pointer
}

Read about signals and slots in this (http://doc-snapshot.qt-project.org/4.8/signalsandslots.html) documentation page, they provide a simpler and more flexible approach than the pointer passing (but then randomFunction must be a member function of another QObject derived class)

QT++
19th August 2012, 06:32
Thankyou for the link I have read it 4 times now and still cant get my head around this. All the examples I can find on youtube relate to console application which I am sure are similar but I just cant get them. Do you know of examples of some working code that I could look at?

Zlatomir
19th August 2012, 09:45
You can look at examples that come with Qt SDK, or maybe you need a book, if so the first edition of C++ gui programming with Qt 4 is a free download from here (http://www.qtrac.eu/marksummerfield.html).

And, of course you can always create a new topic on this forum and ask what you don't understand.

QT++
20th August 2012, 00:31
Worked it out finally, it was really simple and you don't need to pass pointers at all like in your example. Instead you have to declare the functions within the mainwindow.h as private slots of MainWindow class.

mainwindow.h


private slots:
void on_processButton_clicked();
void RandomFunction();


mainwindow.cpp looks like


void MainWindow::on_processButton_clicked(){
// do stuff
MainWindow::randomFunction();
}


secondary.cpp



#includes "mainwindow.h"
#includes "ui_mainwindow.h"

void MainWindow::RandomFunction(){
// do stuff
ui->randomBox->setPlainText("text here");
}


really simple with some example code there if anyone else is having the same issue when 1st coming over from console coding.

Zlatomir
20th August 2012, 04:58
Yes that is another way, but now you declare MainWindow class in .h file and you only partially define it in the corresponding .cpp file, i think this can be confusing. So if you want the separation you require in the first place you pass pointer or make the secondary functionality a class and connect signals and slots.

With the approach you propose right now you can directly do this:


void MainWindow::on_processButton_clicked(){
// do stuff
//MainWindow::randomFunction(); write the code here
//and you don't need second.cpp and randomFunction
ui->randomBox->setPlainText("text here");
}