Results 1 to 8 of 8

Thread: Basic GUI Help

  1. #1
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Basic GUI Help

    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
    Qt Code:
    1. if (1 == 1){
    2. // do stuff
    3. }
    4.  
    5. if(1 == 2){
    6. // do stuff
    7. }
    8.  
    9. if(1 == 3)
    10. {
    11. // do stuff
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. ui->textBox->SetPlainText("words here");
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. ui->textBox->SetPlainText("words here");
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Basic GUI Help

    2) You might want to use 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.

  3. #3
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Basic GUI Help

    Quote Originally Posted by Zlatomir View Post
    2) You might want to use 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

    Qt Code:
    1. void MainWindow::on_processButton_clicked(){
    2. // do stuff
    3. randomFunction();
    4. }
    To copy to clipboard, switch view to plain text mode 

    second.cpp
    Qt Code:
    1. randomFunction(){
    2. // do stuff
    3. ui->randomBox->setPlainText("text here");
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Basic GUI Help

    Quote Originally Posted by QT++ View Post
    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).
    Quote Originally Posted by QT++ View Post
    ...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:
    Qt Code:
    1. void MainWindow::on_processButton_clicked(){
    2. // do stuff
    3. randomFunction(this); //pass the pointer to instance... you can use signals and slots for a more flexible connection
    4. }
    5.  
    6. void MainWindow::UpdateUi(QString updatedText)
    7. {
    8. 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
    9. }
    10.  
    11. //second.cpp
    12. randomFunction(MainWindow* mainWindowInstance){
    13. // do stuff
    14. mainWindowInstance->UpdateUi("text here"); //call the update function with the help of the pointer
    15. }
    To copy to clipboard, switch view to plain text mode 
    Read about signals and slots in this 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)

  5. The following user says thank you to Zlatomir for this useful post:

    QT++ (19th August 2012)

  6. #5
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Basic GUI Help

    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?

  7. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Basic GUI Help

    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.

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

  8. #7
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Basic GUI Help

    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
    Qt Code:
    1. private slots:
    2. void on_processButton_clicked();
    3. void RandomFunction();
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp looks like
    Qt Code:
    1. void MainWindow::on_processButton_clicked(){
    2. // do stuff
    3. MainWindow::randomFunction();
    4. }
    To copy to clipboard, switch view to plain text mode 

    secondary.cpp

    Qt Code:
    1. #includes "mainwindow.h"
    2. #includes "ui_mainwindow.h"
    3.  
    4. void MainWindow::RandomFunction(){
    5. // do stuff
    6. ui->randomBox->setPlainText("text here");
    7. }
    To copy to clipboard, switch view to plain text mode 

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

  9. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Basic GUI Help

    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:
    Qt Code:
    1. void MainWindow::on_processButton_clicked(){
    2. // do stuff
    3. //MainWindow::randomFunction(); write the code here
    4. //and you don't need second.cpp and randomFunction
    5. ui->randomBox->setPlainText("text here");
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Need help with basic Qt
    By doforumda in forum Qt Programming
    Replies: 5
    Last Post: 10th July 2010, 08:53
  2. A few basic question
    By salmanmanekia in forum Newbie
    Replies: 12
    Last Post: 17th June 2010, 07:46
  3. Need basic help
    By Ossi in forum Qwt
    Replies: 2
    Last Post: 23rd October 2009, 13:24
  4. Need immediate help on basic cpp
    By Sandip in forum General Programming
    Replies: 10
    Last Post: 21st September 2008, 10:33
  5. Qt/C++ basic Q-s
    By jamadagni in forum General Programming
    Replies: 9
    Last Post: 10th February 2006, 08:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.