How To Update TableWidget From Another Class?



  • Hello everybody.
    I have a class, child on QWidget, Which lies QTableWidget And written information obtained from the base.
    So, I still have a lot of widgets, which are the base of operations of changing values. QTableWidget only once the information I have, I would like to update the table.
    How can I do to upgrade?



  • @Taz742

    Could be done with signals and slots



  • Hello @the_ Yes, of course I tried it, However, the problem has been created..

    This is My App, when its started:
    alt text

    This table should be visible in some form. Therefore, it is exported to a separate widget.
    Look At:
    alt text
    alt text
    alt text
    This is a picture of three widget.

    Now move the problem on.
    alt text
    When I am making this shape of the object, when I want to get a slot at the end of the operation, the result is:
    ![alt text](0_1492113956653_upload-1dd8c868-cee4-436c-add4-3b2d025735a7 image url)
    This program is the result when i started my app.

    The code is as follows:

        yidvagayidva *yidvaa = new yidvagayidva(this);
    
        QObject::connect(yidvaa,SIGNAL(click()),this,SLOT(sig()));
    
    void MainWidget::sig(){
        qDebug() << "emited";
    }
    
    void yidvagayidva::on_pushButton_2_clicked()
    {
        emit click();
    }
    

    The signal comes from, but the program design is mixed.



  • I tried to get one of the options.

    #ifndef MAINWIDGET_H
    #define MAINWIDGET_H
    
    #include <QWidget>
    #include "QMap"
    #include "yidvagayidva/yidvagayidva.h"
    #include "INPUT_OUTPUT/input_output.h"
    
    class yidvagayidva;
    
    namespace Ui {
    class MainWidget;
    }
    
    class MainWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit MainWidget(QWidget *parent = 0);
        ~MainWidget();
    
        void LoadValues();
    
    public slots:
    
    protected slots:
        void on_tableWidget_cellDoubleClicked(int row, int column);
    
    private:
        Ui::MainWidget *ui;
    };
    
    #endif // MAINWIDGET_H
    
    
    #include "mainwidget.h"
    #include "ui_mainwidget.h"
    #include "QSqlQuery"
    #include "globaldefines.h"
    #include "QDebug"
    #include "QPair"
    #include "globalall.h"
    #include "yidvagayidva/yidvagayidva.h"
    #include "QStandardItem"
    
    MainWidget::MainWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::MainWidget)
    {
        ui->setupUi(this);
    
        ui->tableWidget->setColumnCount(7);
        ui->tableWidget->setRowCount(8);
        ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "სახელი" << "ნაშთი დღის \n დასაწყისში" << "მიღებულია" << "გაცემულია" << "ნაშთი სალაროში" << "ყიდვა" << "გაყიდვა");
        ui->tableWidget->verticalHeader()->hide();
    
        ui->tableWidget->setColumnWidth(0,55);
        ui->tableWidget->setColumnWidth(1,140);
        ui->tableWidget->setColumnWidth(2,140);
        ui->tableWidget->setColumnWidth(3,140);
        ui->tableWidget->setColumnWidth(4,125);
        ui->tableWidget->setColumnWidth(5,70);
        ui->tableWidget->setColumnWidth(6,70);
    
        LoadValues();
    
        for(int i=0; i<ui->tableWidget->rowCount(); ++i){
            ui->tableWidget->verticalHeader()->resizeSection(i,25);
        }
    }
    
    void MainWidget::LoadValues()
    {   
        qDebug() << "movida";
    
        QTableWidgetItem* itm = new QTableWidgetItem(QString::number(globalall->count++));
    
        qDebug() << itm->text();
    
        ui->tableWidget->setItem(2,2,itm);
    
        qDebug() << ui->tableWidget->item(2,2)->text();
    
         for(int i=0; i<3; i++){
             for(int j=5; j<7; j++){
                  QTableWidgetItem* itm2 = new QTableWidgetItem(QString::number(globalall->count++));
                  ui->tableWidget->setItem(i,j,itm2);
             }
         }
    
    }
    

    This is Mainwindow, Which lies Mainwidget and another widget Yidvagayidva.

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "QMap"
    #include "datethread.h"
    #include "kursebi/kursebi.h"
    #include "MainWidget/mainwidget.h"
    #include "yidvagayidva/yidvagayidva.h"
    #include "INPUT_OUTPUT/input_output.h"
    #include "Information/information.h"
    #include "QSpacerItem"
    #include "QLabel"
    
    class MainWidget;
    class yidvagayidva;
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        void RefreshWidget();
    
        QWidget* LastWidget;
        int LastWidgetIndex = 0;
    
        //Widgets
        MainWidget* Main_Widget;
        yidvagayidva* yidva;
        //Widgets End.
    
    protected slots:
        void showEvent(QShowEvent *event);
    
    private slots:
        void on_yidvagayidva_clicked();
    
    private:
        Ui::MainWindow *ui;
        bool bSidebarInitialized;
    };
    
    #endif // MAINWINDOW_H
    
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "globaldefines.h"
    #include "QDebug"
    #include "QSqlQueryModel"
    #include "QSqlQuery"
    #include "user.h"
    #include "kursebi/kursebi.h"
    #include "yidvagayidva/yidvagayidva.h"
    #include "INPUT_OUTPUT/input_output.h"
    #include "QDateTime"
    #include "olduser.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        LastWidget = new QWidget();
    
        Main_Widget = new MainWidget();
        Main_Widget->setMinimumHeight(255);
        ui->background->addWidget(Main_Widget);
    
        bSidebarInitialized = false;
    }
    
    void MainWindow::on_yidvagayidva_clicked()
    {
        globalall->IndicatorCurChange = false;
    
        if(LastWidgetIndex == 1) return;
    
        yidva = new yidvagayidva();
    
        if(LastWidgetIndex > 0)
            delete LastWidget;
    
        LastWidget = yidva;
    
        LastWidgetIndex  = 1;
    
        yidva->setMinimumHeight(430);
        ui->background->addWidget(yidva);
    }
    
    Thus doing tablewidget update.
    void MainWindow::RefreshWidget()
    {
        Main_Widget->LoadValues();
    }
    

    This Is Yidvagayidva widget:

    
    #ifndef YIDVAGAYIDVA_H
    #define YIDVAGAYIDVA_H
    
    #include <QWidget>
    #include "QSqlQueryModel"
    #include "QSqlQuery"
    #include "QRegExpValidator"
    #include "QListWidgetItem"
    #include "mainwindow.h"
    
    class MainWidget;
    
    namespace Ui {
    class yidvagayidva;
    }
    
    class yidvagayidva : public QWidget
    {
        Q_OBJECT
    public:
        explicit yidvagayidva(QWidget *parent = 0);
        ~yidvagayidva();
    
        void RefreshValues();
    
    private slots:
        void on_Complate_clicked();
    
    private:
        Ui::yidvagayidva *ui;
    
    signals:
        CLICKED();
        click();
    };
    
    #endif // YIDVAGAYIDVA_H
    
    void yidvagayidva::RefreshValues()
    {
        MainWindow* mw = static_cast<MainWindow*>(this->parent());
    
        mw->RefreshWidget();
    }
    
    void yidvagayidva::on_Complate_clicked()
    {
            RefreshValues();
    }
    

    When I pushed completebutton result is:
    alt text

    And when I start the program debug mode result is:
    alt text



  • ITS SOLVEEEEEEEEEEED!!!!!!!!!!
    So what was the problem now :)
    yidvagayidva widget has 3 parents, while it is not on destination place.

    void yidvagayidva::RefreshValues()
    {
        MainWindow* mw = static_cast<MainWindow*>(this->parent());
    
        mw->RefreshWidget();
    }
    
        MainWindow* mw = static_cast<MainWindow*>(this->parent()->parent()->parent());
    
        mw->RefreshWidget();
    

  • Lifetime Qt Champion

    Hi,

    Maybe solved but it's quite really very highly nasty. Going up with parent is already bad practice, but 3 level ? That's a recipe for catastrophe.

    If you really need to trigger that function, then you should emit a signal from your yidvagayidva object and connect a slot in your MainWindow to that signal.



  • @SGaist
    Suppose the signal is sent and received slot mainwindow, but I could not understand how to reach out to mainwindow MainWidget Function Called?



  • @SGaist
    i solved my problem as your sad. i had signal in yidvagayidva wich was sent in mainwindow and i deleted yidvagayidva object, before deleting this object i call MainWidget LoadValues();

    void MainWindow::on_yidvagayidva_clicked()
    {
        globalall->IndicatorCurChange = false;
    
        if(LastWidgetIndex == 1) return;
    
        yidva = new yidvagayidva();
    
        if(LastWidgetIndex > 0)
            delete LastWidget;
    
        LastWidget = yidva;
    
        LastWidgetIndex  = 1;
    
        if(Main_Widget->isHidden()) Main_Widget->show();
    
        connect(yidva,SIGNAL(CLICKED()),this,SLOT(hidewidget()));
    
        yidva->setMinimumHeight(430);
        ui->background->addWidget(yidva);
        ui->background->addWidget(spacer);
    }
    
    void yidvagayidva::on_complete_button_clicked(){
          emit clicked();
    }
    
    void MainWindow::hidewidget(){
    
        Main_Widget->LoadValues();
    
        delete LastWidget;
        LastWidgetIndex = 0;
    }
    
    void MainWidget::LoadValues()
    {   
         //reload page;
    }
    

    is this good?


  • Lifetime Qt Champion

    If you are talking about using the CLICKED signal then yes, it looks better than what you have before.



  • @SGaist
    oh yes it is. thank.
    Still, can you explain what exactly is the difference between these two approaches?


  • Lifetime Qt Champion

    Do you mean the difference between going up three parents without any verification that each exists and the proper use of signals and slots ?

    Essentially: proper encapsulation, no tight-coupling.



  • @SGaist
    sheet. I expected more of a time problem, Or something similar.
    Thank for your answer.


  • Lifetime Qt Champion

    What do you mean by time problem ?

    The big time problem that you'll have with such design (the "going up with parents" anti-pattern) is the time you'll lose debugging and maintaining that kind of code.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.