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

problem with QTableWidget and comboBox



  • Hi,

    Let me explain the situation. My program is based in a QDialog that contains a ComboBox and a QTableWidget, and the item selected in the combobox change some non-editable items in the QTableWidget.

    Here is the code

    void GeradorDialog::on_type_box_currentTextChanged(const QString &arg1)
    {
        if(ui->type_box->currentText()=="(V0) Contr. Tensão/Âng."){
            //type_box is the combobox
            item1->setText("Vref");
            unid_item1->setText("pu");
            item2->setText("Aref");
            unid_item2->setText("rad");
            //tabela_fluxo is the QTableWidget
            ui->tabela_fluxo->setItem(0,2,new QTableWidgetItem("1"));
            ui->tabela_fluxo->setItem(1,2,new QTableWidgetItem("0"));
    
    
    
        }
        if(ui->type_box->currentText()=="(PV) Contr. Tensão"){
            item1->setText("P");
            unid_item1->setText("MW");
            item2->setText("Vref");
            unid_item2->setText("pu");
            ui->tabela_fluxo->setItem(0,2,new QTableWidgetItem("0"));
            ui->tabela_fluxo->setItem(1,2,new QTableWidgetItem("1"));
    
    
        }
        if(ui->type_box->currentText()=="(PQ)Sem Controle"){
            item1->setText("P");
            unid_item1->setText("MW");
            item2->setText("Q");
            unid_item2->setText("Mvar");
            ui->tabela_fluxo->setItem(0,2,new QTableWidgetItem("0"));
            ui->tabela_fluxo->setItem(1,2,new QTableWidgetItem("0"));
    
        }
    }
    

    So far so good, in the dialog i have a ok button and after it is triggered it emits a signal that saves al the items texts of the QTableItem in a list

    void GeradorDialog::changevalues(int i, QString value)
    {
        data[i]=value;//data is a QStringList
    }
    
    void GeradorDialog::on_buttonBox_clicked()
    {
    
        QMessageBox::StandardButton resposta=QMessageBox::question(this,"","Deseja salvar esses parametros?",QMessageBox::Yes|QMessageBox::No);
        if(resposta==QMessageBox::Yes)
        {
            changevalues(0,ui->name_line->text());
            changevalues(1,ui->area_line->text());
            changevalues(2,ui->sub_line->text());
            changevalues(3,ui->circuito_line->text());
            changevalues(4,Vnom->text());
            changevalues(5,Snom->text());
            changevalues(6,Item1->text());
            changevalues(7,Item2->text());
            changevalues(8,Pmin->text());
            changevalues(9,Pmax->text());
            changevalues(10,Qmin->text());
            changevalues(11,Qmax->text());
    
            this->close();
    
    }
    

    The problem is that when i click the ok button with the default item in the combo box it works just fine, but if i select another item and press the ok button the porgam crashes. I used Qdebug and it gives me an error in theses lines with the function text:

     changevalues(6,Item1->text());
     changevalues(7,Item2->text());
    

    these QTableItems are changed when i select another item in combo box, but i don't know why it's crashing


  • Lifetime Qt Champion

    Hi,

    How are you changing these values ?
    Looks like you could be re-creating these items rather than updating their content and make these pointers invalid.



  • i'm using setText, why it would make it invalid?


  • Lifetime Qt Champion

    If using only setText it should not.

    You should run the debugger and check exactly what happens to your items while changing their value.

    I saw that you are replacing some while you use setText for others. Check that you are not replacing items that you have pointers to.


Log in to reply