gpt4 book ai didi

C++ - QListWidget 选择第一项

转载 作者:行者123 更新时间:2023-11-30 01:21:03 26 4
gpt4 key购买 nike

在我的 QMainWindow 构造函数中,我读取了一个数据库并用项目填充了我的 QListWidget。显然没有选择任何项目,所以我必须自己做。我还有一个插槽,当我单击列表中的项目时将调用它。

我试过 setCurrentRow( const int ) 但如果我这样做,槽将不会被调用。我看过函数 setCurrentIndex( const QModelIndex & ) 但我不熟悉 QModelIndex。

如何让我的 QListWidget 选择第一项并调用 on_list_clicked(const QModelIndex& index) 插槽?

编辑:此外,我不能使用除 clicked 之外的任何其他插槽,因为当我从列表中删除某个索引时,currentRowChanged(int)itemSelectionChanged() 都会使我的程序崩溃。

所以我需要以某种方式点击列表...

最佳答案

调用 setCurrentRow() 发出信号 currentRowChanged(),它接受 int 而不是 QModelIndex .

只需连接到该信号,而不是连接到 itemSelectionChanged()

示例代码:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->listWidget->setCurrentRow(1);
}

void MainWindow::on_listWidget_currentRowChanged(int currentRow)
{
qDebug() << currentRow;
}

关于C++ - QListWidget 选择第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18596260/

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