如何通知一个小部件有关 Qt 中另一个小部件的更改?

发布于 2024-11-10 18:23:43 字数 1744 浏览 2 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神也荒唐 2024-11-17 18:23:43

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.

把昨日还给我 2024-11-17 18:23:43

OrcunC 给了你一个很好的建议。
如果您是信号/槽实现的新手,您可以从这里开始一些提示。

renderarea.h

signal:
    void pointAdded(QPoint*);

renderarea.cpp

void RenderArea::updateList(QPoint p)
{
    list.append(p);
    emit pointAdded(&list.back());
}

listbox.h

public slots:
    void onPointAdded(QPoint*);

listbox.cpp

void ListBox::onPointAdded(QPoint* point)
{
    //lets assume your ListBox is a QListWidget
    addItem( QString::number(point->x()) + "," + QString::number(point->y()))
}

ListBox 和 RenderArea 的某个实例是可访问

QObject::connect( renderArea, SIGNAL(pointAdded(QPoint*),
                  listBox,    SLOT(onPointAdded(QPoint*)));

注意:命名对于可读性和维护非常重要 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

signal:
    void pointAdded(QPoint*);

renderarea.cpp

void RenderArea::updateList(QPoint p)
{
    list.append(p);
    emit pointAdded(&list.back());
}

listbox.h

public slots:
    void onPointAdded(QPoint*);

listbox.cpp

void ListBox::onPointAdded(QPoint* point)
{
    //lets assume your ListBox is a QListWidget
    addItem( QString::number(point->x()) + "," + QString::number(point->y()))
}

somewhere instance of ListBox and RenderArea are accessible

QObject::connect( renderArea, SIGNAL(pointAdded(QPoint*),
                  listBox,    SLOT(onPointAdded(QPoint*)));

NOTE: nameing is very important for readability and maintenance the void RenderArea::updateList(QPoint p) in this case it's more void RenderArea::addPoint( const QPoint& p) (also notice the const reference telling the compiler that we are not changing p event if we have it's reference)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文