Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Help on C++ GUI programming

    QML and Qt Quick
    3
    6
    2021
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      Nirmala last edited by

      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]

      1 Reply Last reply Reply Quote 0
      • A
        AcerExtensa last edited by

        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”);
        }
        @

        God is Real unless explicitly declared as Integer.

        1 Reply Last reply Reply Quote 0
        • N
          Nirmala last edited by

          Hi AcerExtensa,

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

          Where can i get Example codes..

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

            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");
            }
            @

            1 Reply Last reply Reply Quote 0
            • N
              Nirmala last edited by

              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)

              1 Reply Last reply Reply Quote 0
              • A
                andre last edited by

                [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.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post