QT 4.8.4에서 C++로 프로그래밍 중입니다. 나는 옵션을 선택할 수있는 드롭 다운 메뉴를 원한다. 그러면 exe에 대한 옵션으로 메뉴에서 선택된 항목과 함께 exe를 실행할 것이다.QComboBox가 연결되지 않습니다.
#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
그리고 .cpp 파일 :
#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();
}
지금 난 그냥 작동하지 않는 QComboBox를 사용하는 방법을 알아 내려고 노력하고있어
여기 내 코드입니다. 내 코드가 컴파일되지만 실행하면 Object :: connect : 슬롯이 없습니다. ::: on_go_clicked() "
자습서의 내용을 정확히 설명하고 있습니다. 왜 이것이 작동하지 않는지 나는 알 수 없다.
여분의 GUI를 갖는 액세스하는 데 사용 될 수 있도록, 당신이 슬롯을 수정 :: 부분이 필요합니다 C++에서 오류가 아닙니다. Qt의 신호 및 슬롯 시스템과 관련이 있습니다. 단순히 동일하다는 것을 이해하지 못합니다. – Lochemage
AFAIK, MSVC++ 같은 일부 컴파일러에서는 이와 같은 코드를 작성할 수 있습니다 (추가 자격 사용). 그러나 그것은 표준이 아닙니다. – deepmax
QObject :: connect가 char * 인수를 사용하기 때문에 코드가 컴파일됩니다. 컴파일 할 때 검사가 없습니다. 설명서는 http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#connect – segfault