0

I use QTableWidget to insert data when it is sorting, there has a problem i can't. For example:

QtTest::QtTest(QWidget *parent)
: QMainWindow(parent)
{
    ui.setupUi(this);  

    ui.tableWidget->setSortingEnabled(true);
    ui.tableWidget->sortByColumn(0, Qt::AscendingOrder);
}

void QtTest::on_pushButton_clicked()
{
    static int i = 1;
    ui.tableWidget->insertRow(0);
    ui.tableWidget->setItem(0, 0, new QTableWidgetItem(tr("a%1").arg(i)));
    ui.tableWidget->setItem(0, 1, new QTableWidgetItem(tr("b%1").arg(i+1)));
    i++;
}

then click pushbutton, i can't add data to the row i want, Is there a elegant way to solve it?

frogatto
  • 28,539
  • 11
  • 83
  • 129
handsome
  • 1
  • 3
  • Not sure what the problem is. What are you getting and what do you expect to get? – Jay Nov 11 '17 at 06:26
  • I assume you've read the caveat regarding the use of [`QTableWidget::setItem`](http://doc.qt.io/qt-5/qtablewidget.html#setItem) when sorting is enabled? – G.M. Nov 11 '17 at 11:49
  • I'm so sorry, I did not read the documentation on setItem, but coincidentally my method is same as the help documentation, I ask the question because I thought there has a more elegant way. Thanks a lot. – handsome Nov 11 '17 at 13:53

0 Answers0