如何通知一个小部件有关 Qt 中另一个小部件的更改?
我正在尝试在 Qt 中实现一个小部件,它有 2 个自己的子小部件:一个是渲染区域,我在其中绘制一些点并在它们之间连接线,另一个是一个列表框,我将在其中绘制一些点并连接它们之间的线。就像插入我在渲染区域中绘制的所有点及其坐标的列表。这 2 个小部件是使用 Qt Designer 添加的。这是我到目前为止的代码:
renderarea.h:
class RenderArea : public QWidget
{
Q_OBJECT
public:
RenderArea(QWidget *parent = 0);
QPoint point;
QList<QPoint> list;
protected:
void mousePressEvent(QMouseEvent *);
void paintEvent(QPaintEvent *event);
void updateList(QPoint p);
};
renderarea.cpp:
RenderArea::RenderArea(QWidget *parent)
: QWidget(parent)
{
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
void RenderArea::mousePressEvent(QMouseEvent *e)
{
point = e->pos();
updateList(point);
this->update();
}
void RenderArea::updateList(QPoint p)
{
list.append(p);
}
void RenderArea::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.setPen(QPen(Qt::black,2));
for (int i = 0; i < list.size(); ++i)
painter.drawPoint(list[i]);
if (list.size()>1)
for(int j = 0; j < list.size()-1; ++j)
painter.drawLine(list[j], list[j+1]);
}
paintwidget.h:
class PaintWidget : public QWidget
{
Q_OBJECT
public:
explicit PaintWidget(QWidget *parent = 0);
~PaintWidget();
private:
Ui::PaintWidget *ui;
};
paintwidget.cpp: >
PaintWidget::PaintWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::PaintWidget)
{
ui->setupUi(this);
}
PaintWidget::~PaintWidget()
{
delete ui;
}
我的问题是如何从渲染区域小部件传输到我绘制的另一个点的列表框,并且它应该与其坐标一起显示在列表中?
I am trying to implement a widget in Qt that has 2 child widgets of its own: one is a render area where I draw some points and connect lines between them and the other one is a ListBox where I would like to insert the list of all the points I drew with their coordinates from the render area. The 2 widgets where added with Qt Designer. Here is my code until now:
renderarea.h:
class RenderArea : public QWidget
{
Q_OBJECT
public:
RenderArea(QWidget *parent = 0);
QPoint point;
QList<QPoint> list;
protected:
void mousePressEvent(QMouseEvent *);
void paintEvent(QPaintEvent *event);
void updateList(QPoint p);
};
renderarea.cpp:
RenderArea::RenderArea(QWidget *parent)
: QWidget(parent)
{
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
void RenderArea::mousePressEvent(QMouseEvent *e)
{
point = e->pos();
updateList(point);
this->update();
}
void RenderArea::updateList(QPoint p)
{
list.append(p);
}
void RenderArea::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.setPen(QPen(Qt::black,2));
for (int i = 0; i < list.size(); ++i)
painter.drawPoint(list[i]);
if (list.size()>1)
for(int j = 0; j < list.size()-1; ++j)
painter.drawLine(list[j], list[j+1]);
}
paintwidget.h:
class PaintWidget : public QWidget
{
Q_OBJECT
public:
explicit PaintWidget(QWidget *parent = 0);
~PaintWidget();
private:
Ui::PaintWidget *ui;
};
paintwidget.cpp:
PaintWidget::PaintWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::PaintWidget)
{
ui->setupUi(this);
}
PaintWidget::~PaintWidget()
{
delete ui;
}
My question is how to transmit from the render area widget to my ListBox that I drew another point and it should be displayed along with its coordinates in the list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QT开发中使用的一般方法是使用 signal/slots 进行组件之间的通信软件的。因此,基本上,您需要在源组件(例如 RenderArea 或您喜欢的任何地方)中定义一个信号,并将在另一个组件中定义的插槽(即您的表单窗口)连接到某个位置,并在执行操作时触发信号。
参考链接中也有示例。
The general approach used in QT development is using signal/slots for communication between components of software. So basically you need to define a signal in your source component (for instance RenderArea or whereever your like) and connect your slot defined in another component somewhere (i.e your Form Window) and fire a signal upon an action.
There are examples in the referenced link too.
OrcunC 给了你一个很好的建议。
如果您是信号/槽实现的新手,您可以从这里开始一些提示。
renderarea.h
renderarea.cpp
listbox.h
listbox.cpp
ListBox 和 RenderArea 的某个实例是可访问
注意:命名对于可读性和维护非常重要
void RenderArea::updateList(QPoint p)
在这种情况下它更void RenderArea::addPoint( const QPoint& p)
(另请注意const 引用
告诉编译器,如果我们有它的引用,我们不会更改p
事件)OrcunC gave you a good advice.
If your are new to signal/slots implementation here some hints you can start from.
renderarea.h
renderarea.cpp
listbox.h
listbox.cpp
somewhere instance of ListBox and RenderArea are accessible
NOTE: nameing is very important for readability and maintenance the
void RenderArea::updateList(QPoint p)
in this case it's morevoid RenderArea::addPoint( const QPoint& p)
(also notice theconst reference
telling the compiler that we are not changingp
event if we have it's reference)