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. how to update widgets in a tabwidget
Forum Updated to NodeBB v4.3 + New Features

how to update widgets in a tabwidget

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 504 Views 2 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.
  • M Offline
    M Offline
    Mark58
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #3

      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)));
      
      
      M 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #3

          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)));
          
          
          M 1 Reply Last reply
          1
          • mrjjM mrjj

            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)));
            
            
            M Offline
            M Offline
            Mark58
            wrote on last edited by
            #4

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

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

            and it works.

            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