简体   繁体   中英

Qt qOverload - undeclared identifier

I'm trying to learn to use QComboBox , I tried to compile a chunk of code from https://zetcode.com/gui/qt5/widgets/ but I always get the same error message “ use of undeclared identifier 'qOverload' ” I red all I can find about it on the net, but nothing helped.

I'm using Qt Creator 5.15 and C++11.

combobox.h:

#pragma once

#include <QWidget>
#include <QComboBox>
#include <QLabel>

class ComboBoxEx : public QWidget {

  Q_OBJECT

  public:
    ComboBoxEx(QWidget *parent = nullptr);

  private:
    QComboBox *combo;
    QLabel *label;
};

combobox.cpp:

#include <QHBoxLayout>
#include "combobox.h"

ComboBoxEx::ComboBoxEx(QWidget *parent)
    : QWidget(parent) {

  QStringList distros = {"Arch", "Xubuntu", "Redhat", "Debian",
      "Mandriva"};

  auto *hbox = new QHBoxLayout(this);

  combo = new QComboBox();
  combo->addItems(distros);

  hbox->addWidget(combo);
  hbox->addSpacing(15);

  label = new QLabel("Arch", this);
  hbox->addWidget(label);

  connect(combo, qOverload<const QString &>(&QComboBox::activated),
      label, &QLabel::setText);   // use of undeclared identifier 'qOverload'
}

main.cpp:

#include <QApplication>
#include "combobox.h"

int main(int argc, char *argv[]) {

  QApplication app(argc, argv);

  ComboBoxEx window;

  window.resize(300, 150);
  window.setWindowTitle("QComboBox");
  window.show();

  return app.exec();
}

This is my first question of the forum. Thank you in advance for your help.

activated(const QString &); marked QT_DEPRECATED_SINCE(5, 15) with message Use textActivated() instead , you dont need qOverload<const QString &> for textActivated . Also had troubles with that macro - sometimes it doesn't compile.

I checked the qt documentation for qOverload and you might require C++ 14 and you might need to insert this in your.pro file-> CONFIG += c++14.

Reffer to the link for more details:- http://doc.qt.io/qt-5/qtglobal.html#qOverload

But you can still work around this using the helper methods,since in c++11 you can use the helper functions. Eg:-QOverload<>::of(&Foo::overloadedFunction)

You can read more about it in the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM