Results 1 to 6 of 6

Thread: Multiple Classes setText Update

  1. #1
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple Classes setText Update

    I have two classes: class A and B. class A inherits QWidget and class B inherits class A.

    When a button is clicked, it connects to a slot in class A that creates an object of class B.
    After the http request is done in class B, it executes a function from the base class that changes the text of the button.

    Everything runs except the button doesn't update at all. qDebug shows the label as the new text but on GUI, it is still displaying the old text. I tried update() and repaint() but none works.

    Qt Code:
    1. class A : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. A();
    6. void clickPressed();
    7. private slots:
    8. void changeButtonText();
    9. private:
    10. QPushButton *button;
    11. };
    12.  
    13. A::A()
    14. {
    15. button = new QPushButton(tr("Hello"));
    16. connect(button,SIGNAL(clicked()),this,SLOT(clickPressed()));
    17. }
    18.  
    19. void A::changeButtonText()
    20. {
    21. button->setText(tr("again"));
    22. }
    23.  
    24. void A::clickPressed()
    25. {
    26. B *objectB = new B;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class B : public A
    2. {
    3. Q_OBJECT
    4. public:
    5. B();
    6. private slots:
    7. void requestDone();
    8. private:
    9. QHttp *http;
    10. };
    11.  
    12. B::B()
    13. {
    14. http = new QHttp(this);
    15. ....
    16. connect(http,SIGNAL(done(bool)),this,SLOT(requestDone(bool)));
    17. }
    18.  
    19. B::requestDone(bool error)
    20. {
    21. ...
    22. changeButtonText();
    23. }
    To copy to clipboard, switch view to plain text mode 

    I wrote the above as an example to my problem.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple Classes setText Update

    Could you prepare something we could compile and run? The above code doesn't say much - it seems valid but it could be that the problem is elsewhere, for example you might be blocking the event loop somewhere with a while() (or similar) loop. You could try adding QApplication:rocessEvents() after you change the text and see if it helps. If it does then you are indeed blocking the loop, if not - add a qDebug() statement to see if the slot is executed at all.

  3. #3
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple Classes setText Update

    Ok, here goes. This is the simplified version which represents the problem.

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "a.h"
    3.  
    4. int main(int argc, char *argv[]){
    5.  
    6. QApplication app(argc, argv);
    7.  
    8. A a;
    9. a.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    a.h
    Qt Code:
    1. #ifndef A_H
    2. #define A_H
    3.  
    4. #include <QWidget>
    5. #include <QPushButton>
    6. #include <QVBoxLayout>
    7. #include <QApplication>
    8. #include <qDebug>
    9.  
    10. class A : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. A();
    15. void changeButtonText();
    16.  
    17. public slots:
    18. void buttonPressed();
    19. private:
    20. QPushButton *button;
    21. QVBoxLayout *layout;
    22. };
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    a.cpp
    Qt Code:
    1. #include "a.h"
    2. #include "b.h"
    3.  
    4. A::A()
    5. {
    6. layout = new QVBoxLayout();
    7. setLayout(layout);
    8. button = new QPushButton(tr("try"));
    9. connect(button,SIGNAL(clicked()),this,SLOT(buttonPressed()));
    10.  
    11. layout->addWidget(button);
    12. }
    13.  
    14. void A::buttonPressed()
    15. {
    16. B *ObjectB;
    17. ObjectB = new B;
    18. }
    19.  
    20. void A::changeButtonText()
    21. {
    22. button->setText(tr("here"));
    23. QApplication::processEvents();
    24. qDebug(button->text().toLocal8Bit());
    25. }
    To copy to clipboard, switch view to plain text mode 

    b.h
    Qt Code:
    1. #ifndef B_H
    2. #define B_H
    3.  
    4. #include "a.h"
    5.  
    6. class B : public A
    7. {
    8. Q_OBJECT
    9. public:
    10. B();
    11. };
    12.  
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    b.cpp
    Qt Code:
    1. #include "b.h"
    2.  
    3. B::B()
    4. {
    5. changeButtonText();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Test.pro
    Qt Code:
    1. TEMPLATE = app
    2. CONFIG += qt warn_on embed_manifest_exe console
    3. QT += network
    4. TARGET = Test
    5. SOURCES = main.cpp a.cpp b.cpp
    6. HEADERS = a.h b.h
    7. LIBS +=
    8.  
    9.  
    10. # Treat warnings as errors
    11. win32:QMAKE_CXXFLAGS += /WX
    12.  
    13. CONFIG(debug, debug|release){
    14. # Debug build options
    15. # Enable a read-only console window (i.e. for printf etc.)
    16. # CONFIG += console
    17. }
    18. else{
    19. # Release build options
    20. # Enable a read-only console window (i.e. for printf etc.)
    21. # CONFIG += console
    22. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple Classes setText Update

    Instead of putting everything in codes, I've decided to put it as attachments. Sorry for double posting though. The .pro file is on previous post.
    Attached Files Attached Files
    • File Type: cpp a.cpp (437 Bytes, 1 views)
    • File Type: h a.h (334 Bytes, 1 views)
    • File Type: cpp b.cpp (52 Bytes, 1 views)
    • File Type: h b.h (108 Bytes, 1 views)
    • File Type: cpp main.cpp (157 Bytes, 1 views)

  5. #5
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple Classes setText Update

    cough... I really need guidance on this.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Multiple Classes setText Update

    Well the displayed text couldn't be changed! Why, B creates a new instance of A where the Text is changed. The B::A has nothing to do with your class A created in main().

    If you want to interact between A and B use a proper signal and slot mechanism. (EDIT: Or give the constructor of B a pointer to A)


    Lykurg

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.