从类外部访问小部件
Accessing a widget from outside of its class?
在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.
}
如您所见,我希望再次访问图形场景以更新它,然后重绘它(或其他什么)。但是我根本无法访问图形场景,尽管以不同的方式声明小部件进行了几个小时的反复试验。
我知道侦听器本身是有效的,因为如果它设置为打开一个新的消息框,或输出到调试窗口,那么它就可以工作,只是我无法访问我已经定义的小部件。
我觉得这个问题有一个(相对)简单的解决方案,我只是错过了一些明显的东西。
您将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 文件中,但为了简洁起见,我的示例在声明中这样做。
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()
方法来返回它,您继承了它。
相关文章:
- 在C++中循环访问自定义结构列表的小问题
- Qt小部件中的摄像头访问
- 我们如何准备小内存池,其中每个线程可以独立访问某些位置范围?
- Qt:如何访问继承的小部件?
- QT访问表小部件从单独的线程项目
- QT:类的访问功能,该功能使用构造函数外的GUI小部件
- 几对性能问题(一个较大的向量与较小的块向量),值得存储迭代索引以进行矢量访问
- 从类外部访问小部件
- 从函数访问QTEXTEDIT小部件
- 访问QT小部件线程安全
- 如何从另一个窗口访问窗口的小部件
- 访问QStackWidget中的小部件
- C++ 创建小部件数组
- 从另一个小部件访问主窗口中的文本框
- C++ 从函数访问父小部件
- 从Windows*访问小部件
- 通过按钮访问父窗口小部件的变量
- Qt-如何从另一个子窗口或小工具访问主窗口
- 无法从Qt中的另一个小部件访问小部件中的控件
- 当单击视图中的项时,从另一个小部件访问自定义模型数据