QTableWidget::itemAt() 返回看似随机的项目
我刚刚开始使用 Qt,所以请耐心等待。当我使用 QTableWidget->getItemAt() 时,它返回与我使用 currentItemChanged 并单击同一项目不同的项目。我相信有必要使用 itemAt() 因为我需要获取单击的任何行的第一列。
一些示例代码如下:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QList<QString> rowContents;
rowContents << "Foo" << "Bar" << "Baz" << "Qux" << "Quux" << "Corge" << "Grault" << "Garply" << "Waldo" << "Fred";
for(int i =0; i < 10; ++i)
{
ui->tableTest->insertRow(i);
ui->tableTest->setItem(i, 0, new QTableWidgetItem(rowContents[i]));
ui->tableTest->setItem(i, 1, new QTableWidgetItem(QString::number(i)));
}
}
//...
void MainWindow::on_tableTest_currentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous)
{
ui->lblColumn->setText(QString::number(current->column()));
ui->lblRow->setText(QString::number(current->row()));
ui->lblCurrentItem->setText(current->text());
ui->lblCurrentCell->setText(ui->tableTest->itemAt(current->row(), current->column())->text());
}
对于 1x9 的项目,lblCurrentItem 显示“9”(理应如此),而 lblCurrentCell 显示“Quux”。我做错了什么吗?
I've just started using Qt, so please bear with me. When I use QTableWidget->getItemAt(), it returns a different item from if I used currentItemChanged and clicked the same item. I believe it's necessary to use itemAt() since I need to get the first column of whatever row was clicked.
Some example code is below:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QList<QString> rowContents;
rowContents << "Foo" << "Bar" << "Baz" << "Qux" << "Quux" << "Corge" << "Grault" << "Garply" << "Waldo" << "Fred";
for(int i =0; i < 10; ++i)
{
ui->tableTest->insertRow(i);
ui->tableTest->setItem(i, 0, new QTableWidgetItem(rowContents[i]));
ui->tableTest->setItem(i, 1, new QTableWidgetItem(QString::number(i)));
}
}
//...
void MainWindow::on_tableTest_currentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous)
{
ui->lblColumn->setText(QString::number(current->column()));
ui->lblRow->setText(QString::number(current->row()));
ui->lblCurrentItem->setText(current->text());
ui->lblCurrentCell->setText(ui->tableTest->itemAt(current->row(), current->column())->text());
}
For the item at 1x9, lblCurrentItem displays "9" (as it should,) whereas lblCurrentCell displays "Quux". Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Qt 文档说:
QTableWidgetItem * QTableWidget::itemAt ( int ax, int ay ) const
返回表部件坐标系中相当于 QPoint(ax, ay) 的位置处的项目,或者如果指定点未被覆盖则返回 0表小部件中的一个项目。
另请参见 item()。
所以你可能应该使用 item(row, column) 代替:
ui->lblCurrentCell->setText(ui->tableTest->item(current->row(), current->column())->text());
Qt documenration says:
QTableWidgetItem * QTableWidget::itemAt ( int ax, int ay ) const
Returns the item at the position equivalent to QPoint(ax, ay) in the table widget's coordinate system, or returns 0 if the specified point is not covered by an item in the table widget.
See also item().
So you should probably use item(row, column) instead:
ui->lblCurrentCell->setText(ui->tableTest->item(current->row(), current->column())->text());
看起来您的表格正在按照第 0 列(“Foo,Bar,...”)进行排序。这样一来,'Q'uux 排在 9 岁,早于 Waldo 就有意义了。要么在第 0 列插入数字,要么禁用排序,否则我认为您明白了。有很多解决方案。
Looks like your table is getting sorted as per the 0th ("Foo, Bar, ...") column. That way 'Q'uux being at 9, before Waldo makes sense. Either insert the numbers at 0th column or disable sorting or I think you got the point. There are many solutions.