Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel text
Qt 6.11 is out! See what's new in the release blog

QLabel text

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 7.1k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi,

      qDebug() can be used in such cases.

      Regards,

      Debao

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

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

        So, what is that error exactly?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4
          • 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
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @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
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved