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.
AND:
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.
}
而且正如你所看到的,我希望再次访问图形场景来更新它,然后重绘i吨(或其他)。但是我根本无法访问图形场景,尽管用不同的方式声明小部件需要几个小时的试验和错误。我知道监听器本身是可以工作的,因为如果它设置为打开一个新的消息框,或者输出到调试窗口,那么它就可以工作,这只是我不能访问我已经定义的小部件。
我觉得这个问题有一个(相对)简单的解决方案,而且我只是错过了一些明显的东西。
只是让他们成员变量的类和提供getter方法... – cmannett85
@ cmannett85 - 你能解释这个更详细/提供一个例子吗? (我对C++相当陌生!)谢谢! – BnMcG
Bret Kuhns做得很好。不过,我必须强调,这不是学习C++的方法!与'普通'C++相比,Qt使用了一些不常见/不可用的范例,所以先学习语言,然后再学习框架。 – cmannett85