如何加载QSettings当修复QTableWidget的的setText / setCellWidget导致崩溃

问题描述 投票:0回答:1

您好我使用的是QTableWidget,基本上有一些代码使得第一列将使表增长/收缩动态。最后填充行的第一个单元格将有一个加号按钮添加一个新行,可以编辑和减号按钮将删除其相应的按钮的排。例如,如果我点击加号按钮的5倍,这将是在第6行和前5排都将有一个减号按钮来删除其行。如果单元格的列上有一个减号按钮,该行暗示可编辑的,否则不是。然而,我让表有行的默认量,使得它可以在视觉上从未超过5收缩更小,例如,即使是技术上的“空”。要编辑的行,你需要打加号按钮,将检查,如果我们需要插入新行,例如,如果我们在5行的基行大小为5,那么我们需要一个新的行和填充每列索引新行的。

此表的主要原因是阅读和表的信息写入QSettings并能够导入过去导出的信息相同的细胞,基本上恢复表的状态,因为它出现在去年。不过,从QSettings读取时,该表将崩溃所有细胞与信息无误后,我可以告诉他们正确填充,因为调试程序将冻结GUI,我可以直观地看到多远qDebug()成的环QSettings阵列我之前SEG-断层。

现在,当我手工做的一切,如点击加号按钮,一切都很好,并预期该程序执行。

//newRowCount is assumed to be the amount of rows with Minus Buttons currently, only creates a new row if the table needs to expand
void TableTest::enableNewRow(){
    newRowCount++;
    if(newRowCount > table->rowCount()){
        table->insertRow(table->rowCount());
        for(int i = 0; i < table->columnSize(); ++i){
            QTableWidgetItem* item = new QTableWidgetItem;
            item->setFlags(item->flags() ^ Qt::ItemisEditable);
            table->setItem(newRowCount, i, item);
            //Code to set the previous column's item (newRowCount--) to have an Qt::ItemisEditable flag set to true
            ...
        }
    }
        //Some button setup to make Plus button and create a new Minus button connect and do their jobs and move the Plus button down into the newly inserted row
        ...
}
void MainWindow::importFile(){
    int settingRows = settings->beginReadArray("Groups");
    for(int i = 0; i < settingRows; ++i){
        settings->setArrayIndex(i);
        //Will only add a new row if needed, check inside above function and all row items will be allocated if made
        table->enableRow();
        for(int j = 1; j < table->columnCount(); ++j){
            table->item(i, j)->setText("testing");
        }
    }
}

And the program crashes with the following stack trace:
1  QWidget::show()
2  QAbstractItemView::updateEditorGeometries()
3  QAbstractItemView::updateGeometries()
4  QTableView::updateGeometries()
5  QTableView::timerEvent(QTimerEvent *)
6  QObject::event(QEvent *)
7  QWidget::event(QEvent *)
8  QFrame::event(QEvent* )
9  QAbstractScrollArea::event(QEvent* )
10 QAbstractItemView::event(QEvent* )
11-21 ... Not useful information about any of the code
22 QCoreApplication::exec()
23 main

然而,它的所有字节码,我不能实际检查哪一行崩溃的代码在任何使用调试器的痕迹。我不明白的是,手动点击加号按钮被完成(我连加号按钮向enableRow()功能的单击事件),当我编程方式从enableRow()循环中调用QSettingsimportFile()会崩溃相同的过程后遍历所有的项目。

注意事项:表中的基底大小设置为QSettings阵列我从阅读的大小(即,如果我想10行,只设置10行与项目设置的文本将正常工作之前开始),然而,我想表在创建基本尺寸是这样的,这一点似乎一旦环路超越我从构造函数中指定的行数,然后程序会崩溃,但不是在说,指数6,但之后只有它通过该表已经完全循环。它没有任何行崩溃,如QTableWidgetItem::setTextQTableWidget::setCellWidget()。我有点糊涂,不知道如果表被填充得太快对堆栈跟踪行QTableWidget::timerEvent 5.我没有在循环做比正常操作的表时,我做的有什么不同。

qt qtablewidget qtablewidgetitem qsettings
1个回答
0
投票

事实证明,错误是,我是一个行比我早应该已经设置QTableWIdgetItem。我不知道为什么表将能够重复,直到崩溃前的QSettings循环结束,但根本上是错误。我不明白为什么堆栈跟踪是如此神秘。

在我的enableRow()函数,我实际上从上面张贴存储器的正确逻辑,这在我的代码的逻辑不同由1个索引先前针对行值。为什么用其插槽调用相同的功能import()似乎难不倒我,也许是不确定的行为的情况下,该按钮正常使用时的代码没有崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.