2017-07-17 18 views
0

私のアプリケーションは2つのクラスを使用するため、2つのクラス "ファイル"を持ちます。ファーストクラスはボタンを表示します。2番目のクラスは矩形を描画し、キーを押すと特定の矩形の色を変更します。 2番目のクラスにはpaintEventメソッドが含まれています。プログラムは正常にコンパイルされますが、四角形は表示されません。私はアプリケーションを終了するテストメソッドを作成します。
ファーストクラス、私は間違って何をやっているpaintEventメソッドを呼び出さないクラス

#include "renderArea.h" 
#include <QPainter> 
#include <QApplication> 
#include <QPushButton> 
renderArea::renderArea(QWidget *parent) 
    : QWidget(parent) 
{ 

    setAutoFillBackground(true); 
    setFixedSize(40, 40); 
    //this->update(); 
} 
void renderArea::paintEvent(QPaintEvent*) { 
    QPainter painter(this); 
    QRect a = QRect(90, 230, 70, 40); 
    QRect s = QRect(215, 230, 70, 40); 
    QRect d = QRect(340, 230, 70, 40); 
    QRect w = QRect(215, 150, 70, 40); 
    painter.setPen(Qt::black); 
    painter.drawText(a, Qt::AlignCenter, "a"); 
    painter.drawText(s, Qt::AlignCenter, "s"); 
    painter.drawText(d, Qt::AlignCenter, "d"); 
    painter.drawText(w, Qt::AlignCenter, "w"); 
    painter.drawRect(a); 
    painter.drawRect(s); 
    painter.drawRect(d); 
    painter.drawRect(w); 
} 
void renderArea::test() { 
    QApplication::instance()->quit(); 
} 
#pragma once 
#include <QWidget> 
class QPushButton; 
class renderArea : public QWidget 
{ 
    Q_OBJECT 
public: 
    enum Keys {w,a,s,d}; 
    renderArea(QWidget *parent=0); 
    void test(); 
protected: 
    void paintEvent(QPaintEvent *event) override; 
private: 
    Keys keys; 
    QPushButton *button; 
}; 

クラスRCcarに作成されます(main.cppにによって呼び出される1)

#include "RCcar.h" 
    #include "renderArea.h" 
    #include <QPushButton> 
    #include <QApplication> 
    RCcar::RCcar() 
    { 
     Renderarea = new renderArea; 
     Renderarea->test(); 
     //Renderarea->update(); 
     exit = new QPushButton("Exit", this); 
     exit->setGeometry(410, 440, 80, 50); 
     connect(exit, SIGNAL(released()), QApplication::instance(), SLOT(quit())); 
     setFixedSize(500, 500); 
    } 
#pragma once 

#include <QWidget> 
class QPushButton; 
class renderArea; 
class RCcar : public QWidget 
{ 
    Q_OBJECT 

public: 
    RCcar(); 
private: 
    QPushButton *exit; 
    renderArea *Renderarea; 
}; 

セカンドクラス?あなたのコードで
おかげ

+0

'RCcar'ウィジェットは' renderArea'ウィジェットの親であるべきではありませんか?おそらくあなたはそのように設定する必要がありますか? –

答えて

1

2個のエラーがあります:あなたはそれが後者に描かれているように、それは、親としてのウィジェットをrenderArea合格しなければならないレイアウトを使用するつもりはないので

  1. が。

  2. renderAreaコンストラクタでは、サイズを40 * 40に設定し、paintEventメソッドを描画すると、その領域を描画しています。

修正は親としてthisに渡され、適切なサイズに設定されます。

Renderarea = new renderArea(this); 
Renderarea->setGeometry(0, 0, 500, 400); 
exit = new QPushButton("Exit", this); 
exit->setGeometry(410, 440, 80, 50); 
connect(exit, SIGNAL(released()), QApplication::instance(), SLOT(quit())); 
setFixedSize(500, 500); 

そしてラインsetFixedSize(40, 40);を削除します。

renderArea::renderArea(QWidget *parent) : QWidget(parent) 
{ 
    setAutoFillBackground(true); 
} 

スクリーンショット:

enter image description here

:paintEventメソッドが呼び出されますが、不適切なスペースに描いていました。

+0

2番目の間違いはタイプミスでしたが、ええ、私は今、 – lulz

関連する問題