Home > OS >  Can't get an item from tableWidget in QT
Can't get an item from tableWidget in QT

Time:10-20

I have the function like this below, and global QVector<pid_t> pid; in the header file which elements are Linux process ids. But when I'm trying to push the button "priority" - programm unexpectedly finishes. Due to qDebugs I've realized that function interrupts after if statement. And I can not understand the matter of this problem. Function:

void MainWindow::on_priority_clicked()
    {
        int curI = ui->tableWidget->currentRow();
        int prio = ui->prioritySpinBox->value();
        try{
            if(ui->tableWidget->item(curI,1)->text().isNull())
                throw curI;
            else {
            setpriority(PRIO_PROCESS, pid.at(curI),prio);
            QLabel *labelPrio = new QLabel(ui->tableWidget);
            labelPrio->setText(QString::number(getpriority(PRIO_PROCESS, pid.at(curI))));
            ui->tableWidget->setCellWidget(curI, 3, labelPrio);
            }
        }
        catch(int x)
        {
            QMessageBox::warning(this, "Error", "Process "   QString::number(x 1)   " is not created");
        }
    }

CodePudding user response:

Not sure if this is your problem, but if ui->tableWidget->item(curI,1) doesn't exist (or is null), then calling ->text() on it will cause a crash. You might need to check if it exists first:

void MainWindow::on_priority_clicked()
    {
        int curI = ui->tableWidget->currentRow();
        int prio = ui->prioritySpinBox->value();
        try{
            if(ui->tableWidget->item(curI,1) != nullptr)
                ....
  •  Tags:  
  • c qt
  • Related