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

Retrieve data from widget in QTab



  • Hi, I can't recover a value in a LineEdit inserted in a QTableWidget that is inserted in QTab.
    I searched the solution for a time and I found some answers, but nothing that work for me.
    I've a QTab in class inputWindow tha call class STable that make a** QTableWdiget** with LineEdit in first column.
    Then the QTableWidget (tbl) generated from class STable is invisible to class inputWindow where QTab (tab) and B2 button is.
    The B2 slot it should retrieve data from tbl and save in a file.
    I think that QTab has a reference to tbl and in turn to LineEdit, but I can't found this.
    Can you help me?
    Following you can see the code:

    #include "mainwindow.h"
    #include "inputwindow.h"
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QTabWidget>
    #include <QPushButton>
    #include <QDebug>
    #include <QTableWidget>
    #include <QBrush>
    #include <QColor>
    
    InputWindow::InputWindow(QWidget *parent) : QWidget(parent)
    {
        QVBoxLayout *vb = new QVBoxLayout(this);
        QHBoxLayout *hb1 = new  QHBoxLayout;
    
        QPushButton *b1 = new QPushButton("Chiudi");
        QObject::connect(b1, SIGNAL(released()), this, SLOT(b1Pressed()));
    
        QPushButton *b2 = new QPushButton("B2");
        QObject::connect(b2, SIGNAL(released()), this, SLOT(b2Pressed()));
    
        QPushButton *b3 = new QPushButton("B3");
        hb1->addWidget(b1);
        hb1->addWidget(b2);
        hb1->addWidget(b3);
        vb->addLayout(hb1);
    
        QHBoxLayout *hb2 = new  QHBoxLayout;
        tab = new QTabWidget(this);  //tab defined in header
        tab->addTab(new STable, "Test1");
        tab->addTab(new STable, "Test2");
    
        hb2->addWidget(tab);
        vb->addLayout(hb2);
    
        QHBoxLayout *hb3 = new QHBoxLayout;
        QPushButton *bbb1 = new QPushButton("BBB1");
        QPushButton *bbb2 = new QPushButton("BBB2");
        QPushButton *bbb3 = new QPushButton("BBB3");
        QPushButton *bbb4 = new QPushButton("BBB4");
        QPushButton *bbb5 = new QPushButton("BBB5");
        QPushButton *bbb6 = new QPushButton("BBB6");
        QPushButton *bbb7 = new QPushButton("BBB7");
        QPushButton *bbb8 = new QPushButton("BBB8");
    
        hb3->addWidget(bbb1);
        hb3->addWidget(bbb2);
        hb3->addWidget(bbb3);
        hb3->addWidget(bbb4);
        hb3->addWidget(bbb5);
        hb3->addWidget(bbb6);
        hb3->addWidget(bbb7);
        hb3->addWidget(bbb8);
    
        vb->addLayout(hb3);
    }
    
    void InputWindow::b1Pressed()
    {
        emit(destroyed());
        InputWindow::close();
        qDebug() << "b1Pressed";
    }
    
    void InputWindow::b2Pressed()
    {
        qDebug() << "b2pressed";
        
        //Proc to save tablewidgets content;
    }
    
    STable::STable(QWidget *parent) : QWidget(parent)
    {
        QPushButton *b = new QPushButton(nullptr);
        b->setText("calc");
        QObject::connect(b, SIGNAL(released()), this, SLOT(bPressed()));
    
        /*QTableWidget*/ tbl = new QTableWidget(100,7); //tbl defined in header
    
        tbl->setHorizontalHeaderLabels(QStringList()<<"Descrizione"<<"aaa"<<"bbb"<<"ccc"<<"ddd"<<"eee"<<"fff");
    
        tbl->setColumnWidth(0, 460);
        tbl->setColumnWidth(1, 60);
        tbl->setColumnWidth(2, 140);
        tbl->setColumnWidth(3, 90);
        tbl->setColumnWidth(4, 90);
        tbl->setColumnWidth(5, 90);
        tbl->setColumnWidth(6, 20);
    
        for(int i = 0; i < tbl->rowCount(); i++)
        {
            tbl->setRowHeight(i, 10);
    
            //tbl->setStyleSheet("<style='color:#ff0000'>");
            //if(i & 0x01) tbl->item(i+1,1)->background().color().red(); //Crash
    
            //tbl->setCellWidget(i, 0, SetString());
            tbl->setCellWidget(i, 1, SetNumber("0"));
            tbl->setCellWidget(i, 2, SetComboBox());
            tbl->setCellWidget(i, 3, SetNumber("0"));
            tbl->setCellWidget(i, 4, SetNumber("0"));
            tbl->setCellWidget(i, 5, SetNumber("0"));
            tbl->setItem(i, 6, SetCheckBox(""));
        }
    
        QVBoxLayout *Layout = new QVBoxLayout;
        Layout->addWidget(tbl);
        Layout->addWidget(b);
        setLayout(Layout);
    }
    
    void STable::bPressed()
    {
        qDebug() << "bPressed";
        qDebug() << tbl->item(0,0); //Print a pointer
    }
    
    // ******************
    QLineEdit *STable::SetString(void)
    {
        QLineEdit *tableline = new QLineEdit;
        tableline->setMaxLength(60);
        tableline->setFrame(false);
    
        //QRegExp rx("[0-9][A-Z][a-z]{0,60}");
        //QRegExp rx("[A-Z]{0,60}[/+()\"'= -]");
        //rx.setCaseSensitivity(Qt::CaseInsensitive);
    
        //tableline->setValidator(new QRegExpValidator(rx, tableline));
        return tableline;
    }
    
    QLineEdit *STable::SetNumber(QString value)
    {
        QLineEdit *tableline = new QLineEdit;
        tableline->setMaxLength(5);
        tableline->setText(value);
        tableline->setFrame(false);
        tableline->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
        QRegExp rx("[0-9]{5}");
        tableline->setValidator(new QRegExpValidator(rx, tableline));
        return tableline;
    }
    
    QTableWidgetItem *STable::SetCheckBox(QString value)
    {
        QHBoxLayout *hb = new QHBoxLayout;
        QTableWidgetItem* item =  new QTableWidgetItem;
        item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
        item->setCheckState(Qt::Unchecked);
        return item;
    }
    
    QComboBox *STable::SetComboBox(void)
    {
        QComboBox *comboBox = new QComboBox;
        comboBox->addItem("1");
        comboBox->addItem("2");
        comboBox->addItem("3");
        comboBox->addItem("4");
        comboBox->addItem("5");
        comboBox->addItem("6");
        comboBox->addItem("7");
        comboBox->addItem("8");
        comboBox->addItem("9");
        comboBox->addItem("10");
    
        return comboBox;
    }
    
    

    Thanks.


  • Lifetime Qt Champion

    Hi,

    Add an API to that widget that will return the data you are interested in. No need to expose implementation details.



  • Hi SGaist, I don't understand what you asked me.


  • Lifetime Qt Champion

    Rather than trying to go down in the bowels of that widget, add a method to it the returns the data you are interested in.


Log in to reply