[SOLVED] tableWidget causes the program to finish unexpectedly



  • Hi. I am trying to add in fields to a table widget. I have a widget created in my .ui file, and I'm trying to add to it. For some reason, the program is quitting unexpectedly without adding the object.

    My code is:
    @
    ui->tableWidget->insertRow(0);
    //QCheckBox* checkBox = newQCheckBox; //commented out until I can at least create an empty row
    //ui->tableWidget->setCellWidget(0,0,checkBox);
    @

    Does anyone know why this would cause an unexpected crash with no error message displayed? If not, are there any methods to debug this problem?


  • Lifetime Qt Champion

    Hi,

    You didn't set any QTableWidgetItem on the new row so it can't contain anything



  • I'm sorry, I don't quite understand. I thought the insertRow command only took the one integer as an augment. Is the QTableWidgetItem set before or after the insertRow?


  • Moderators

    where does the program crash?
    Run it in QtCreator (for example) and post the stack trace of the crash.
    Hard to tell where the cause is with just a single line of code.



  • The program crashes inside the insertRow line. I added
    @
    qDebug() << "Before Insert";
    ui->tableWidget->insertRow(0);
    qDebug() << "After Insert";
    @

    and my output is
    Starting C:\blabla\TheMAPStore...
    Before Insert
    The program has unexpectedly finished.
    C:\blabla\TheMAPStore exited with code -1073741819

    I didn't notice that error code before. I'll look into that and see if it explains things for me.

    EDIT: It seems this error code has to do with missing dll's. I am using #include's to include QTableWidget and QCheckBox. Do I need to manually add those dll's to my project file? If so, where are they found?


  • Moderators

    did you probably forgot to call
    @ui->setupUi(this);@
    in your constructor?

    Please show some more code...



  • main.cpp:
    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    MainWindow w;
    w.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    mainwindow.cpp:
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->tableWidget->insertRow(0);
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @



  • Oh, I'm an idiot. setupUi(this) needs to come first. Thanks for everyone help!


  • Moderators

    you try to access the tablewidget before you are initializing it with setupUi() ...


Log in to reply
 

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