2013-08-15 2 views
0

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() "

자습서의 내용을 정확히 설명하고 있습니다. 왜 이것이 작동하지 않는지 나는 알 수 없다.

답변

4

gui::을 제거

class gui : public QDialog{ 
    Q_OBJECT 

... 

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

왜 내가 코드를 컴파일할지 궁금합니다. 'on_go_clicked 회원에 대한 추가 자격 없음'. 헤더의 on_go_clicked에서 gui ::를 삭제하십시오.

+0

여분의 GUI를 갖는 액세스하는 데 사용 될 수 있도록, 당신이 슬롯을 수정 :: 부분이 필요합니다 C++에서 오류가 아닙니다. Qt의 신호 및 슬롯 시스템과 관련이 있습니다. 단순히 동일하다는 것을 이해하지 못합니다. – Lochemage

+0

AFAIK, MSVC++ 같은 일부 컴파일러에서는 이와 같은 코드를 작성할 수 있습니다 (추가 자격 사용). 그러나 그것은 표준이 아닙니다. – deepmax

+0

QObject :: connect가 char * 인수를 사용하기 때문에 코드가 컴파일됩니다. 컴파일 할 때 검사가 없습니다. 설명서는 http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#connect – segfault

1

당신은이 QComboBox 객체는 참조가 있습니다.

첫 번째는 클래스 수준에있다 :

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 
}; 

두 번째 문제를 해결하려면 생성자

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

    ... 

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

에 존재하는 지방 포인터 - 투 - QComboBox 개체, 당신은 변경할 수 있습니다 클래스 수준의 객체를 포인터로 만든 다음 객체 생성을 선언 및 초기화 대신 간단한 할당으로 변경합니다. 또한

cb = new QComboBox(); 

,이 작업을 완료 한 후 포인터 참조 연산자가 text() 기능

void gui::on_go_clicked(){ 
    QMessageBox::information(this, "ASDF", cb->currentText()); 
} 
+0

또한 생성자에서'QComboBox'를'hLayout2'에 할당하고 나중에'vLayout'에 다시 할당합니다. 당신은 당신이 원하는 레이아웃을 결정해야합니다. – RobbieE

+0

오, 그건 목적이 아니 었습니다. 나는 그것을 작동 시키려고 노력하는 것들을 재배치하는 동안 그것을 방금 떠났다. –

관련 문제