如何从 QCombobox 中获取选定的 VALUE?

发布于 2024-08-17 21:02:12 字数 561 浏览 6 评论 0原文

在 Qt 中,我可以使用以下命令获取 QComboBox选定文本 combobox->currentText() 方法。 如何获取所选值

我寻求帮助,但找不到我期望找到的方法 currentData() 。我只能找到 combobox->currentIndex()

除了 combobox->itemData(combobox->currentIndex()) 之外,还有更聪明的方法吗?

更新:从 Qt 5 开始不再需要此操作。添加了 currentData() 方法 http://doc.qt.io/qt-5/qcombobox.html#currentData-prop

In Qt, I can get the selected text of a QComboBox by using the
combobox->currentText() method.
How can I get the selected value?

I searched for help but I couldn't find a method currentData() which I expected to find. I could only find combobox->currentIndex()

Is there a smarter way to do it other than combobox->itemData(combobox->currentIndex())?

Update: This is no longer necessary as of Qt 5. A currentData() method has been added http://doc.qt.io/qt-5/qcombobox.html#currentData-prop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

猥琐帝 2024-08-24 21:02:13

我有同样的问题,

我已经解决了

value = self.comboBox.currentText()
print value

I had same issue

I have solved by

value = self.comboBox.currentText()
print value
深海里的那抹蓝 2024-08-24 21:02:13

这是我在 QT 4.7 中的 OK 代码:

 //add combobox list 
    QString val;
   ui->startPage->clear();
    val = "http://www.work4blue.com";
    ui->startPage->addItem(tr("Navigation page"),QVariant::fromValue(val));
    val = "https://www.google.com";
    ui->startPage->addItem("www.google.com",QVariant::fromValue(val));
    val = "www.twitter.com";
    ui->startPage->addItem("www.twitter.com",QVariant::fromValue(val));
    val = "https://www.youtube.com";
    ui->startPage->addItem("www.youtube.com",QVariant::fromValue(val));

   // get current value
    qDebug() << "current value"<< 
       ui->startPage->itemData(ui->startPage->currentIndex()).toString();

This is my OK code in QT 4.7:

 //add combobox list 
    QString val;
   ui->startPage->clear();
    val = "http://www.work4blue.com";
    ui->startPage->addItem(tr("Navigation page"),QVariant::fromValue(val));
    val = "https://www.google.com";
    ui->startPage->addItem("www.google.com",QVariant::fromValue(val));
    val = "www.twitter.com";
    ui->startPage->addItem("www.twitter.com",QVariant::fromValue(val));
    val = "https://www.youtube.com";
    ui->startPage->addItem("www.youtube.com",QVariant::fromValue(val));

   // get current value
    qDebug() << "current value"<< 
       ui->startPage->itemData(ui->startPage->currentIndex()).toString();
守护在此方 2024-08-24 21:02:13

我很惊讶没有激活信号并且有同样的问题。我通过创建 QComboBox 的子类解决了这个问题。我认为最好避免直接访问对象并调用其函数,因为这意味着更紧密的耦合并且违背了 Qt 的理念。这是我制作的适合我的课程。

class SmartComboBox : public QComboBox {

    Q_OBJECT

private slots:

    void triggerVariantActivated(int index);

public:

    SmartComboBox(QWidget *parent);

signals:

    void activated(const QVariant &);

};

以及实施

void SmartComboBox::triggerVariantActivated(int index)
{
    activated(itemData(index));
}

SmartComboBox::SmartComboBox(QWidget *parent)
:QComboBox(parent)
{
    connect(this, SIGNAL(activated(int)), this, SLOT(triggerVariantActivated(int)));
}

I'm astonished that there isn't an activated signal and have the same problem. I solved it by making a subclass of QComboBox. I think it's better to avoid having to directly access the object and call its functions because that means more tight coupling and goes against Qt's philosophy. So here's the class I made that works for me.

class SmartComboBox : public QComboBox {

    Q_OBJECT

private slots:

    void triggerVariantActivated(int index);

public:

    SmartComboBox(QWidget *parent);

signals:

    void activated(const QVariant &);

};

And the implementation

void SmartComboBox::triggerVariantActivated(int index)
{
    activated(itemData(index));
}

SmartComboBox::SmartComboBox(QWidget *parent)
:QComboBox(parent)
{
    connect(this, SIGNAL(activated(int)), this, SLOT(triggerVariantActivated(int)));
}
憧憬巴黎街头的黎明 2024-08-24 21:02:13

这个问题很老了,但也许有人需要一个实际的答案。

在 QGIS 3.4 中,您可以使用 currentData() 方法从 QComboBox 获取值。

示例:comboBox.currentData()

链接: https://doc.qt.io/qt-5/qcombobox.html#currentData-prop

The question is old, but maybe, somebody need an actual answer.

In the QGIS 3.4 you can get the value from the QComboBox with the method currentData().

Example: comboBox.currentData()

Link: https://doc.qt.io/qt-5/qcombobox.html#currentData-prop

小忆控 2024-08-24 21:02:13

我这样做了

QDir path("/home/user/");
QStringList _dirs = path.entryList(QDir::Dirs);
std::cout << "_dirs_count = " << _dirs.count() << std::endl;
ui->cmbbox->addItem(Files);
ui->cmbbox->show();

,您将看到名为 _dirs 的 QStringList 的结构类似于一个数组,您可以通过索引访问其成员,最多可达 _dirs.count() 返回的值

I did this

QDir path("/home/user/");
QStringList _dirs = path.entryList(QDir::Dirs);
std::cout << "_dirs_count = " << _dirs.count() << std::endl;
ui->cmbbox->addItem(Files);
ui->cmbbox->show();

You will see with this that the QStringList named _dirs is structured like an array whose members you can access via an index up to the value returned by _dirs.count()

毅然前行 2024-08-24 21:02:13

我遇到了这个问题并

QString str = m_UI->myComboBox->currentText();

解决了这个问题。

I had the issue and

QString str = m_UI->myComboBox->currentText();

solved this.

在巴黎塔顶看东京樱花 2024-08-24 21:02:13

如果您正在开发 QGIS 插件,那么只需

self.dlg.cbo_load_net.currentIndex()

if you are developing QGIS plugins then simply

self.dlg.cbo_load_net.currentIndex()
信仰 2024-08-24 21:02:13

我知道我已经迟到了,但对于那些仍然有这个问题的人来说,它可以很容易地解决。
我使用 Qt 5.3 并且运行良好。无需创建函数或其他所有内容。

int valueComboBox;
valueComboBox = comboBox->currentIndex();

它有效!
希望有帮助!

I know I'm very late but for those who still have that problem, it can be solved easily.
I use Qt 5.3 and it works fine. No need to create a function or all that.

int valueComboBox;
valueComboBox = comboBox->currentIndex();

and it works !
Hope it helps !

冰之心 2024-08-24 21:02:13

我确认最简单的方法是这样做:

uiAnalyseAssets::AnalyseAssets(QWidget *parent)
: QWidget(parent)
{
ui.comboBox->addItem("text1");
ui.comboBox->addItem("text2");

...
}

void mainFunction::yourFunction( int index )
{
 int value = ui.comboBox->currentText();
}

I confirm the easiest way is to do this:

uiAnalyseAssets::AnalyseAssets(QWidget *parent)
: QWidget(parent)
{
ui.comboBox->addItem("text1");
ui.comboBox->addItem("text2");

...
}

void mainFunction::yourFunction( int index )
{
 int value = ui.comboBox->currentText();
}
平生欢 2024-08-24 21:02:12

如果您想获取 QComboBox 的当前数据,似乎您需要执行 combobox->itemData(combobox->currentIndex())

如果您使用自己的从 QComboBox 派生的类,则可以添加 currentData() 函数。

It seems you need to do combobox->itemData(combobox->currentIndex()) if you want to get the current data of the QComboBox.

If you are using your own class derived from QComboBox, you can add a currentData() function.

花伊自在美 2024-08-24 21:02:12

这个可以获取当前索引的文本:

QString cb = cbChoice ->currentText();

This one can get the text of current index:

QString cb = cbChoice ->currentText();
楠木可依 2024-08-24 21:02:12

您可以为所有项目设置QVariant数据,然后您可以在需要时获取该值。

对于这种情况有一个示例代码:

ui.comboBoxSheetSize->addItem("128 m", QVariant(128));
ui.comboBoxSheetSize->addItem("256 m", QVariant(256));
ui.comboBoxSheetSize->addItem("512 m", QVariant(512));
ui.comboBoxSheetSize->addItem("1024 m", QVariant(1024));

...

void Page::onComboSheetSizeChanged( int index )
{
 int value = ui.comboBoxSheetSize->itemData(index).toInt();
}

顺便说一下,我想我误解了你的问题。我认为你获取数据的方式足够聪明吗?

you can set QVariant data for all items, then you can get the value when you need it.

there is an example code for this situation:

ui.comboBoxSheetSize->addItem("128 m", QVariant(128));
ui.comboBoxSheetSize->addItem("256 m", QVariant(256));
ui.comboBoxSheetSize->addItem("512 m", QVariant(512));
ui.comboBoxSheetSize->addItem("1024 m", QVariant(1024));

...

void Page::onComboSheetSizeChanged( int index )
{
 int value = ui.comboBoxSheetSize->itemData(index).toInt();
}

by the way, i think i misunderstood your question. i think the way you get data is smart enough?

淡墨 2024-08-24 21:02:12

自从提出这个问题以来,成员函数 QComboBox::currentData 已被添加,请参阅此 commit

The member function QComboBox::currentData has been added since this question was asked, see this commit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文