Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    QLabel text

    General and Desktop
    4
    7
    5541
    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.
    • E
      eXevio last edited by

      Hello guys i am pretty new to Qt and you may find my question silly.

      I have created 3 simple files
      main.cpp
      TextController.h
      TextController.cpp

      What i do , is i have a QLabel in the main Widget and i want to print the contents of the QLabel .
      i tried in the TextController.h to get the Qlabel text by--> QString text = myLabel->text() and then cout<<text<<endl; but i get compile error.
      I have searched all of over the net but nothing :/

      //TextController.h
      @#include <qobject.h>
      class TextController : public QObject
      {
      Q_OBJECT
      public:
      TextController();
      private slots:
      void Print();
      };@

      // TextControllet.cpp
      @#include "TextController.h"
      #include <iostream>
      #include <qstring.h>

      using namespace std;
      TextController::TextController()
      {
      QString labelText = myLabel->text();
      }
      void TextController::Print()
      { //print()

      cout<<"Label Text Goes Here!"<<endl;
      } //print()@

      // main
      @#include <qapplication.h>
      #include <qlabel.h>
      #include <qpushbutton.h>
      #include "TextController.h"

      int main(int argc, char *argv[])
      {
      QApplication myApp(argc,argv);

      QWidget *myWidget = new QWidget;
      myWidget->setGeometry(400,300,120,90);

      TextController myTextController;

      QLabel *myLabel = new QLabel("Hello World!",myWidget,0);
      QPushButton *myPrintButton = new QPushButton("Print",myWidget);
      QPushButton *myQuitButton = new QPushButton("Exit",myWidget);

      myLabel->setGeometry(10,10,100,30);
      myPrintButton->setGeometry(10,50,100,30);
      myQuitButton->setGeometry(10,90,100,30);

      QObject::connect(myPrintButton,SIGNAL(clicked()),&myTextController,SLOT(Print()));
      QObject::connect(myQuitButton,SIGNAL(clicked()),&myApp,SLOT(quit()));
      myWidget->show();
      return myApp.exec();
      }@

      any help would be appreciated

      1 Reply Last reply Reply Quote 0
      • D
        dbzhang800 last edited by

        Hi,

        qDebug() can be used in such cases.

        Regards,

        Debao

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

          quote author="eXevio" date="1332294119" but i get compile error. [/quote]

          So, what is that error exactly?

          1 Reply Last reply Reply Quote 0
          • L
            lgeyer last edited by

            • Qt headers are usually included using <code>#include <QtModule/QClass></code>, not <code>#include <qclass.h></code>
            • You are missing a call to the parent constructor in TextController.cpp #6
            • labelText is a local variable in TextController #8
            • myLabel is not a member in TextController #8
            • Qt widgets are usually positioned using layout managers, not absolute positioning

            labelText has to be a member, so it is accessible in TextController::Print().

            myLabel is unknown to TextController; it has to be passed to TextController (for example in the constructor or using a setter) and stored in a member variable.

            1 Reply Last reply Reply Quote 0
            • E
              eXevio last edited by

              @TextController.cpp: In constructor ‘TextController::TextController()’:
              TextController.cpp:8: error: ‘myLabel’ was not declared in this scope
              TextController.cpp:8: warning: unused variable ‘labelText’
              TextController.cpp: In member function ‘void TextController::Print()’:
              TextController.cpp:13: error: ‘labelText’ was not declared in this scope
              make: *** [TextController.o] Error 1
              @

              and my TextController.cpp looks like:
              @#include "TextController.h"
              #include <iostream>
              #include <qstring.h>

              using namespace std;
              TextController::TextController()
              {
              QString labelText = myLabel->text();
              }
              void TextController::Print()
              { //print()

              cout<<labelText<<endl;
              } //print()@

              1 Reply Last reply Reply Quote 0
              • E
                eXevio last edited by

                myLabel is unknown to TextController; it has to be passed to TextController (for example in the constructor or using a setter) and stored in a member variable.[/quote]
                Thanks For the info Lukas, i could figure out that but the question is how to implement it :o

                1 Reply Last reply Reply Quote 0
                • L
                  lgeyer last edited by

                  There are various ways - depending on the design of your application. The obvious one is passing the label to TextController in the constructor, using a setter or a combination of both.
                  @
                  class TextController
                  {
                  public:
                  TextController(QLabel *label) : label_(label)
                  {
                  }

                  void setLabel(QLabel *label)
                  {
                      label_ = label;
                  }
                  
                  QLabel *label() const
                  {
                      return label_;
                  }
                  
                  void Print()
                  {
                      if(label_ != 0)
                      {
                          cout << label->text().toStdString();
                      }
                  }
                  

                  private:
                  QLabel *label_;
                  }

                  ....

                  TextController myTextController(myLabel);
                  ....
                  myTextController.setLabel(myLabel);
                  @

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