Pregunta

I'm programming in QT 4.8.4 with C++. I want to have a drop-down menu, where I can select an option, and then it will run an exe with the selected item on the menu as the option for the exe.

Here's my code:

#ifndef GUI_H
#define GUI_H

#include <QDialog>
#include <QtGui>

class QLabel;
class QLineEdit;
class QPushButton;

class gui : public QDialog
{
    Q_OBJECT

public:
    gui(QWidget *parent = 0);

public slots:
    void gui::on_go_clicked();

private:
    QLabel *label1;
    QLabel *label2;
    QLineEdit *lineEdit;
    QPushButton *goButton;
    QComboBox cb;
};

#endif

And the .cpp file:

#include <QtGui>
#include <QApplication>
#include <QComboBox>

#include "gui.h"

#include <vector>

gui::gui(QWidget *parent) : QDialog(parent)
{
    label1 = new QLabel(tr("Insert Name (Optional):"));
    label2 = new QLabel(tr("Class Name (Required):"));
    lineEdit = new QLineEdit;

    goButton = new QPushButton(tr("&Go"));
    goButton->setDefault(true);
    connect(goButton, SIGNAL(clicked()), this, SLOT(on_go_clicked()));

    QComboBox *cb = new QComboBox();
    cb->addItem("Hello", "1");
    cb->addItem("Test", "2");

    QHBoxLayout *hLayout1 = new QHBoxLayout;
    hLayout1->addWidget(label1);
    hLayout1->addWidget(lineEdit);

    QHBoxLayout *hLayout2 = new QHBoxLayout;
    hLayout2->addWidget(label2);
    hLayout2->addWidget(cb);

    QHBoxLayout *hLayout3 = new QHBoxLayout;
    hLayout3->addWidget(goButton);
    hLayout3->addStretch();

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addLayout(hLayout1);
    vLayout->addLayout(hLayout2);
    vLayout->addWidget(cb);
    vLayout->addLayout(hLayout3);

    setLayout(vLayout);
    setWindowTitle(tr("TEST"));
    setFixedHeight(sizeHint().height());
}

void gui::on_go_clicked()
{
    QMessageBox::information(this, "ASDF", cb.currentText());
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    gui *stuff = new gui;
    stuff->show();
    return app.exec();
}

Right now I'm just trying to figure out how to use QComboBox, which isn't working. My code compiles, but when I run it I get "Object::connect: No such slot gui::on_go_clicked()"

I'm doing exactly what a tutorial says to do. I can't figure out why this isn't working.

¿Fue útil?

Solución

Remove gui:: :

class gui : public QDialog{
    Q_OBJECT

...

public slots:
    void gui::on_go_clicked();
         ^^^^^
         Remove it

Otros consejos

I wonder why you code even compiles. No 'extra qualification on member on_go_clicked'. Remove gui:: from on_go_clicked in your header.

You have two QComboBox objects that you reference.

The first is at the class level:

class gui : public QDialog{
    Q_OBJECT

public:
    gui(QWidget *parent = 0);

public slots:
    void gui::on_go_clicked();

private:
    QLabel *label1;
    QLabel *label2;
    QLineEdit *lineEdit;
    QPushButton *goButton;
    QComboBox cb;           // <<<=== class-level automatic object
};

The second is a local pointer-to-QComboBox object that exists in the constructor

gui::gui(QWidget *parent) : QDialog(parent){

  ...

  QComboBox *cb = new QComboBox();  // <<<=== function-level pointer using the same name 
                                    //        as the class-level automatic object

To correct the problem, you can change the class-level object to be a pointer and then change the object creation to be a simple assignment instead of a declaration-and-initialisation.

cb = new QComboBox();

Also, once you've done this, you'll need to modify the slot so that the pointer dereference operator is used to access the text() function

void gui::on_go_clicked(){
  QMessageBox::information(this, "ASDF", cb->currentText());
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top