Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to update widgets in a tabwidget



  • Hi
    I have a tabwidget within my mainwindow. The tabwidget has a QLabel which should be changed. In the mainwindow I have a QLineEdit and a QPushbutton. When I enter a text in the QLineEdit und press the button, the label in the tabwidget should get the value of the lineedit.
    The ui of the tabwidget with the QLabel is in a seperate file.
    However, the QLabel does not change. Signal/Slot is working, because "qDebug" prints "hallo".

    widget.JPG

    my mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "tab_todo.h"
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    signals:
        void sendmessage(QString);
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
        tab_todo mydo;
    };
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->tabWidget->addTab(new tab_todo, "Action_here");
    
        connect(this, SIGNAL(sendmessage(QString)),&mydo,SLOT(receiver(QString)));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
        QString messagetext = ui->lineEdit->text();
        sendmessage(messagetext);
    }
    

    tab_to.h

    #ifndef TAB_TODO_H
    #define TAB_TODO_H
    
    #include <QWidget>
    #include <QDebug>
    
    namespace Ui {
    class tab_todo;
    }
    
    class tab_todo : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit tab_todo(QWidget *parent = nullptr);
        ~tab_todo();
    
    
    private slots:
        void receiver(QString message);
    
    
    private:
        Ui::tab_todo *ui;
    };
    
    #endif // TAB_TODO_H
    

    tab_todo.cpp

    #include "tab_todo.h"
    #include "ui_tab_todo.h"
    
    tab_todo::tab_todo(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::tab_todo)
    {
        ui->setupUi(this);
    
    }
    
    tab_todo::~tab_todo()
    {
        delete ui;
    }
    
    void tab_todo::receiver(QString message)
    {
        qDebug() << "now at receiver" << "message:" << message;
        ui->label->setText(message);
    }
    

    Thanks for your help.


  • Lifetime Qt Champion

    Hi
    Adding to @SGaist good points.

    The reason the text is not changing is that you have 2 of them

    tab_todo mydo; <<< in .h instance 1
    
    //  here you create a new one and not use the one from .h
        ui->tabWidget->addTab(new tab_todo, "Action_here");  
    
    // then connect to the one from .h that is not shown on screen.
     connect(this, SIGNAL(sendmessage(QString)),&mydo,SLOT(receiver(QString)));
    
    

  • Lifetime Qt Champion

    Hi,

    Give your tab_todo class the proper API to do that. Your MainWindow should not care where the text is shown. It should just set it on that widget and that one is responsible to forward it where appropriate.


  • Lifetime Qt Champion

    Hi
    Adding to @SGaist good points.

    The reason the text is not changing is that you have 2 of them

    tab_todo mydo; <<< in .h instance 1
    
    //  here you create a new one and not use the one from .h
        ui->tabWidget->addTab(new tab_todo, "Action_here");  
    
    // then connect to the one from .h that is not shown on screen.
     connect(this, SIGNAL(sendmessage(QString)),&mydo,SLOT(receiver(QString)));
    
    


  • @mrjj Thank you
    Your answer was helpful and understandable.
    I changed the .h - file to:

    ui->tabWidget->addTab(&mydo, "Action_here");
    

    and it works.


Log in to reply