August 6, 2012

Nirmala Nirmala
Lab Rat
4 posts

help on C++ GUI programming

 

Hi All,

Am new to this QT Creator. Trying to write the programs. Where can i get Basic Example programs.. Am stuck in Pushbutton program i don’t know how to proceed..
Kindly help on this…

Program is when you press the button then it should display(LED ON)
if you press the button again then it should display(LED OFF)

Program is here:

  1. void MainWindow::PushButtonHandler1()
  2. {
  3.     ui->label1->setText("LED1 ON");
  4. }

when i click the button LED1 ON text is displayed.
If i click the button second time i want the output LED1 OFF to be displayed.
How to use if condition, what all parameters i should set ??

[edit, code tags added, koahnig]

5 replies

August 6, 2012

AcerExtensa AcerExtensa
Robot Herder
570 posts

simply check the text of the button:

  1. void MainWindow::PushButtonHandler1()
  2. {
  3. if(ui->label1->text() == “LED1 ON”)
  4. {
  5. ui->label1->setText(“LED1 OFF”);
  6. return;
  7. }
  8. ui->label1->setText(“LED1 ON”);
  9.  }

 Signature 

God is Real unless explicitly declared as Integer.

August 6, 2012

Nirmala Nirmala
Lab Rat
4 posts

Hi AcerExtensa,

Thank you so much for the code its working as requied..

Where can i get Example codes..

August 6, 2012

Andre Andre
Area 51 Engineer
6031 posts

While it works, I would recommend that you don’t get into the habbit of using GUI elements like labels to store application state. Instead, store the state elsewhere, like in a member variable. Yes, it will look a bit more complicated now, but it is a good habbit to get into. Also, it would be good to as much as makes sense give methods names that refer to what they do, instead of to what triggers them. You might want to add a second trigger later for the same action, and then you’re stuck with a method name that doesn’t fit anymore.

So, I would suggest:

  1. //in header:
  2. private:
  3.    bool m_led1On;
  4.  
  5. //in source:
  6. void MainWindow::toggleButton() {
  7.   m_led1On = !m_led1On; //toggle led
  8.  
  9.   if (m_led1On) {
  10.     ui->label1->setText("LED1 ON");
  11.   } else {
  12.     ui->label1->setText("LED1 OFF");
  13.   }
  14.  
  15.   //this would do the same:
  16.   // ui->label1->setText(m_led1On ? "LED1 ON" : "LED1 OFF");
  17. }

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 6, 2012

Nirmala Nirmala
Lab Rat
4 posts

Hello Andre,

Thank you for the suggestions…i will try to adopt these methods..

If i want to communicate with the hardware how can i Transmit and/or Receive data(Ex. establishing Bluetooth communication)

August 6, 2012

Andre Andre
Area 51 Engineer
6031 posts

Nirmala wrote:
Hello Andre,

Thank you for the suggestions…i will try to adopt these methods..


You’re welcome.

If i want to communicate with the hardware how can i Transmit and/or Receive data(Ex. establishing Bluetooth communication)

Please ask new questions in new topics. However, have a look at Qt Mobility Connectivity [doc.qt.nokia.com] for starters.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ QML TextEdit: issues while writing      Is it possible to emit a signal from an QML ListModel element? ››

You must log in to post a reply. Not a member yet? Register here!