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:
    @
    void MainWindow::PushButtonHandler1()
    {
    ui->label1->setText("LED1 ON");
    }
    @
    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]



  • simply check the text of the button:
    @
    void MainWindow::PushButtonHandler1()
    {
    if(ui->label1->text() == “LED1 ON”)
    {
    ui->label1->setText(“LED1 OFF”);
    return;
    }
    ui->label1->setText(“LED1 ON”);
    }
    @



  • Hi AcerExtensa,

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

    Where can i get Example codes..



  • 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:
    @
    //in header:
    private:
    bool m_led1On;

    //in source:
    void MainWindow::toggleButton() {
    m_led1On = !m_led1On; //toggle led

    if (m_led1On) {
    ui->label1->setText("LED1 ON");
    } else {
    ui->label1->setText("LED1 OFF");
    }

    //this would do the same:
    // ui->label1->setText(m_led1On ? "LED1 ON" : "LED1 OFF");
    }
    @



  • 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)



  • [quote author="Nirmala" date="1344252487"]Hello Andre,

    Thank you for the suggestions...i will try to adopt these methods..
    [/quote]
    You're welcome.

    [quote]If i want to communicate with the hardware how can i Transmit and/or Receive data(Ex. establishing Bluetooth communication) [/quote]
    Please ask new questions in new topics. However, have a look at "Qt Mobility Connectivity":http://doc.qt.nokia.com/qtmobility/connectivity-api.html for starters.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.