从类外部访问小部件?

时间:2012-12-15 18:16:43

标签: c++ qt

在Qt Creator中,我有几个小部件声明如下:

标题文件:

    class MapViewer : public QGraphicsView
{
    Q_OBJECT

public:
    explicit MapViewer(QGraphicsScene *scene, QWidget *parent = 0);
    ~MapViewer();

public slots:
    void mousePressEvent(QMouseEvent *event);

};

// Declaration for the map editor window.
class MapEditor : public QMainWindow
{
    Q_OBJECT

public:
    explicit MapEditor(QWidget *parent = 0);
    ~MapEditor();

public:
    QLayout *editorLayout;
    QPushButton *btn;
    QGraphicsScene *mapScene;
    MapViewer *mapView;

private:
    Ui::MapEditor *ui;
};

CPP文件:

  MapEditor::MapEditor(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MapEditor)
{

    ui->setupUi(this);
    this->setWindowTitle("2DXY :: Map Editor");
    this->setGeometry(10,10,1170,750);
    editorLayout = new QVBoxLayout; // Create a new layout
    this->setLayout(editorLayout); // Set the widget's layout to our newly created layout.

    mapScene = new QGraphicsScene(); // Create a new graphics scene to draw upon.

    mapView = new MapViewer(mapScene,this); // Create a new graphics view to display our scene - set its parent to 'this' so that it doesn't open in a new window.
    mapView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mapView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mapView->setGeometry(20,20,1178,546); // Width first, then height.

void MapViewer::mousePressEvent(QMouseEvent *event)
{
    // Show an empty message box, just to check that the event handler works!
    QMessageBox *notification = new QMessageBox();
    notification->show();
    notification->exec();
    // Some how access the same QGraphicsScene and View (mapScene, mapView) as above, so
    // I can update their contents on the open form / window.

}

正如您所看到的,我希望再次访问图形场景以更新它,然后重绘它(或其他)。但是我无法访问图形场景,尽管在以不同方式声明小部件的几个小时的试验和错误。

我知道侦听器本身是可行的,因为如果它设置为打开一个新的消息框,或者输出到调试窗口,那么它可以工作,只是我无法访问我已经定义的小部件。 / p>

我觉得这个问题有一个(相对)简单的解决方案,我只是遗漏了一些明显的东西。

2 个答案:

答案 0 :(得分:1)

您已将QGraphicsScene传递给MapRender对象的构造函数。你在构造函数中对场景做了什么?理想情况下,您应该将其存储为MapRender的数据成员。例如:

class MapRender {
public:
    MapRender(QGraphicsScene* scene)
      : scene_(scene)
    {
    }

public slots:
    void mousePressEvent(QMouseEvent *event);

private:
    QGraphicsScene* scene_;
}

现在,在mousePressEvent的实施中,您可以访问场景成员:

void MapRender::mousePressEvent(QMouseEvent *event) {
    int CursorX = event->globalX();
    int CursorY = event->globalY();

    QGraphicsRectItem *clickedBox = new QGraphicsRectItem(40,40,32,32);
    clickedBox->setBrush(QBrush(Qt::blue));

    scene_->addItem(clickedBox);
}

请记住,理想情况下,您应该将构造函数的实现放在cpp文件中,但我的示例在声明中为了简洁起见。

答案 1 :(得分:0)

void MapViewer::mousePressEvent(QMouseEvent *event)
{
    // Show an empty message box, just to check that the event handler works!
    QMessageBox *notification = new QMessageBox();
    notification->show();
    notification->exec();

    //  To add something whenever the user clicks, you don't need the view,
    //  just the scene.
    scene()->addItem( new MyItem() );
}

请记住MapViewer来自QGraphicsView并且视图必须知道它所属的场景 - 所以它有一个scene()方法来返回它,你继承了它。