2014-10-30 3 views
-1

OpenGL을 사용하여 생성 된 삼각형과 그 삼각형 색상을 변경하는 세 개의 누름 단추가있는 간단한 응용 프로그램을 만들고 싶었습니다. 삼각형이 생성되지 않습니다하지만 불행히도 버튼이 작동하지 않습니다 내가 말하는 오류를 얻을 :Qt, OpenGL 위젯, 슬롯 없음

QObject를이 : 연결 : 없음 같은 슬롯 MainWindow를 :: redSlot (OGL)를 .. \ buttonwidget에 mainwindow.cpp \ : 17 QObject :: connect : 해당 슬롯 없음 MainWindow :: greenSlot (OGL) in .. \ buttonwidget \ mainwindow.cpp : 20 QObject :: connect : 슬롯 없음 MainWindow :: blueSlot (OGL) in .. \ buttonwidget \ mainwindow.cpp : 23

내가 가지고있는 슬롯 정의 :

void MainWindow::redSlot(Widget* w) 
{ 
    w->setColor(red); 
} 

void MainWindow::greenSlot(Widget* w) 
{ 
    w->setColor(green); 
} 

void MainWindow::blueSlot(Widget* w) 
{ 
    w->setColor(blue); 
} 

생성 된 삼각형의 색상을 변경하는 클래스 위젯에서 선언 된 변수를 변경하고 있습니다. 여기에 클래스 위젯의 :

class Widget : public QGLWidget 
{ 
    Q_OBJECT 

public: 
    Widget(QWidget *parent = 0); 

    QSize minimumSizeHint() const; 
    QSize sizeHint() const; 

    enum color c; 

    void setColor(enum color color1); 

protected: 
    void initializeGL(); 
    void paintGL(); 
    void resizeGL(int width, int height); 

}; 

그리고 나는 수업 시간에 버튼에 슬롯을 연결 MainWindow를 생성자 :

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) 
{ 
    layout = new QVBoxLayout(); 

    QWidget *w = new QWidget(); 
    setCentralWidget(w); 

    w->setLayout(layout); 

    Widget *OGL = new Widget(); 

    //OGL->c=green; - it was a test whether changing value of enum type variable c works 
    //it works, changing it changes the color of a generated triangle 

    redButton = new QPushButton(tr("Red")); 
    connect(redButton, SIGNAL(clicked()), this, SLOT(redSlot(OGL))); 

    greenButton = new QPushButton(tr("Green")); 
    connect(greenButton, SIGNAL(clicked()), this, SLOT(greenSlot(OGL))); 

    blueButton = new QPushButton(tr("Blue")); 
    connect(blueButton, SIGNAL(clicked()), this, SLOT(blueSlot(OGL))); 

    layout->addWidget(OGL); 
    layout->addWidget(redButton); 
    layout->addWidget(greenButton); 
    layout->addWidget(blueButton); 
} 

여기에 헤더 슬롯 선언입니다 :

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

    QVBoxLayout *layout; 

public: 
    MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 


private: 
    QPushButton *redButton; 
    QPushButton *greenButton; 
    QPushButton *blueButton; 

public slots: 
    void redSlot(Widget*); 
    void greenSlot(Widget*); 
    void blueSlot(Widget*); 

}; 

가 어떻게 그들이 작동되도록해야합니까? QT에서

+0

당신이'MainWindow' 헤더에 선언하기 전에 슬롯 키워드를 넣어 않았다 클린을하고 –

+0

를 다시 한 경우에, Qt를 사용한 이후로 꽤 오랜 시간이 걸렸지 만, 나는이 오류를 모호하게 기억하고 있습니다. MOC 컴파일러를 올바르게 사용 했습니까? 헤더를'public slot :'을 사용하여 정확하게 정의 했습니까? – kroneml

+0

홍수에 빠지기 전에 Qt 기초 지식을 배우고 자합니다 : http://qt-project.org/doc/qt-5/signalsandslots.html. 문제가있는 경우 실제 프로젝트에서 코드 조각을 게시하는 대신 SSCCE에 전체 코드를 제공해야합니다. –

답변

1

넥트 즉, 문자열 기반 있습니다

connect(redButton, SIGNAL(clicked()), this, SLOT(redSlot(OGL))); 

당신이 슬롯 "redSlot (OGL)"를 정의 havent 한 이후로 대신 사용 할 것, 작동하지 않습니다

...SLOT(redSlot(Widget*))); 

또한, 같이하는 것이 당신의 슬롯/연결, 적은 매개 변수가 아니라 더와 SLOT을 정의 할 수있다이다

void redSlot(); 

connect(redButton, SIGNAL(clicked()), this, SLOT(redSlot())); 

"위젯"에 대한 포인터가 필요한 경우 다른 곳에서 가져와야합니다.

예 : "위젯"- 클래스에 슬롯을 정의하고에 변화가 연결 :

connect(redButton, SIGNAL(clicked()), OGL, SLOT(redSlot())); 
+0

그러면 위젯 OGL에 어떻게 영향을 줍니까? 삼각형의 색상을 변경하기 위해 변수의 값을 변경해야하고 다른 해결책을 제시 할 수 없습니다. – user30935

+0

MainWindow 대신 "위젯"클래스에 슬롯을 정의하면 관련 데이터에 직접 액세스 할 수 있습니다. 그것은 또한 훨씬 더 깨끗하게 될 것입니다 (적어도 저에게는) – Robert

+0

OK, 그건 정말로 의미가 있습니다.내 위젯 클래스에서 슬롯을 정의했지만 여전히 다음과 같은 오류가 발생합니다. 슬롯이 없습니다. MainWindow :: greenSlot() greenSlot()이 MainWindow 클래스에 더 이상 없기 때문에 놀랍습니다. – user30935