我在运行时有一个字符串列表。
任何人都可以帮助我在 QWidget 中显示这些字符串。当我右键单击该字符串时,我需要一个选项显示索引,它将在 QMessageBox 中显示该字符串的索引。
如果可能的话,请提供一些技术指导。
谢谢你。
我在运行时有一个字符串列表。
任何人都可以帮助我在 QWidget 中显示这些字符串。当我右键单击该字符串时,我需要一个选项显示索引,它将在 QMessageBox 中显示该字符串的索引。
如果可能的话,请提供一些技术指导。
谢谢你。
好的,让我们开始为您的用例设计...
我建议使用 QListWidget 作为列表。每个字符串可以是一个单独的项目。
您可以显示一个右键单击弹出窗口,但如果它只有显示索引操作,它本身并没有那么大的意义。您可以立即显示带有该索引的消息框。
我会在下面写这样的东西:
MyClass::MyClass(QObject *parent)
: QObject(parent)
, m_listWidget(new QListWidget(this))
{
QStringList myStringList = QStringList() << "foo" << "bar" << "baz";
m_listWidget->addItems(myStringList);
// Set up your action with the popup for right click if needed
// and connect to the "triggered" signal of the particular action
connect(listWidget, SIGNAL(itemClicked(QListWidgetItem * item)), SLOT(showMessageBox(QListWidgetItem * item)));
...
}
void MyClass::showMessageBox(QListWidgetItem * item)
{
Q_UNUSED(item)
QMessageBox messageBox;
messageBox.setText(m_listWidget->currentRow());
messageBox.exec();
}
如果您有更多右键单击操作,您可以使用弹出对话框,是的,其中放置了几个操作,但到目前为止,这似乎不是这里的用例。