gpt4 book ai didi

c++ - QTableWidget::setItem 上的 Qt4 SegmentationFault

转载 作者:行者123 更新时间:2023-11-28 07:41:55 25 4
gpt4 key购买 nike

我这里有一个很奇怪的问题。当我将项目设置到表格时,我的程序是 SegmentationFault。这是我的代码。

标题:

class Program : public QMainWindow {
Q_OBJECT
public:
Program();

private:
QTableWidget *table;

private slots:
void newSlot();
}

Cpp 文件:

Program::Program() : QMainWindow() {
....
....
....
....
table = new QTableWidget();
table->setRowCount( 0 );
table->setColumnCount( 2 );
....
....
....
}

void Program::newSlot() {
....
....
....
table->insertRow( table->rowCount() );
table->setItem( table->rowCount() - 1, 0, new QTableWidgetItem( "something" ) );
table->setItem( table->rowCount() - 1, 1, new QTableWidgetItem( "something" ) );
....
....
....
}

问题是当程序到达 newSlot() 中的 table->setItem( ... ) 时,我遇到了段错误。我是不是在某个地方犯了一些愚蠢的错误导致了这场困惑? '因为我在其他地方使用了完全相同的代码,没有任何问题。

最佳答案

你必须指定列数:

table->setColumnCount( 2 );

table->setItem( table->rowCount() - 1, 0, new QTableWidgetItem( "something" ) );
...

关于c++ - QTableWidget::setItem 上的 Qt4 SegmentationFault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15682001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com