如何从Qt中的另一个类访问变量?

发布于 2024-11-13 17:39:22 字数 2256 浏览 1 评论 0原文

我正在尝试在 Qt 中实现一个主窗口,它有 2 个小部件:一个区域是我绘制一些点的区域,另一个是我在其中写入所有点及其各自坐标的列表框。我想在主窗口上实现按钮的“删除点”功能,即当我按下按钮时,从列表框中选择的点应该从我正在绘制的区域消失。因此,我正在考虑使用信号/槽来执行此操作,但是当我尝试从绘图区域访问我的点列表时,它只是找不到任何包含的数据。这是我到目前为止的代码:

paintwidget.cpp(我的主窗口):


PaintWidget::PaintWidget(QWidget parent) :
        QWidget(parent),
        ui(new Ui::PaintWidget)
{
    area = new RenderArea(this);
    ui->setupUi(this);
    connect(ui->displayWidget, SIGNAL(listUpdated(QList)), ui->pointsListWidget,
            SLOT(onListUpdated(QList*)));
    connect(ui->deletePoints, SIGNAL(clicked()), this, SLOT(deleteItem()));
}
void PaintWidget::deleteItem()
{
    area->deletePoint(ui->pointsListWidget->currentItem());
}

renderarea.cpp(我的绘图区域):


void RenderArea::mousePressEvent(QMouseEvent *e)
{
    point = e->pos();
    updateList(point);
    this->update();
}
void RenderArea::updateList(const QPoint& p)
{
    list.append(p);
    if (list.count()>1)
        lineAdded(p);
    emit listUpdated(&list);
}
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]);
}
void RenderArea::deletePoint(QListWidgetItem *item)
{
    bool ok1;
    bool ok2;
    int index = item->text().indexOf(",");
    int x = item->text().left(index).toInt(&ok1, 10);
    int y = item->text().mid(index + 1).toInt(&ok2, 10);
    for (int i = 0; i < list.size(); ++i)
        //find the point with x and y as coordinates and delete it
}

列表框.cpp:


void ListBox::onListUpdated(QList *list)
{
    clear();
    for (int i = 0; i < list->size(); ++i)
        addItem(new QListWidgetItem(QString::number(list->at(i).x()) + ", " +
                                    QString::number(list->at(i).y())));
}

渲染区域中的列表是 QPoints 的 QList。问题是在 FOR 循环中列表的大小为 0,所以我看不到它应该包含的任何点。我认为我无法在某个地方初始化它,但我不确定在哪里。

这些点是用 QPainter 绘制的,所以当我从列表中删除点时是否有可能从我的绘图区域中删除它们?

I am trying to implement in Qt a main window which has 2 widgets: one area where I draw some points and one list box where I write all the points with their respective coordinates. And I would like to implement the function "delete point" of a button on the main window, i.e. when I press the button then the point selected from the list box should disappear from my area where I am drawing. So I was thinking of doing this with signals/slots, but when I try to gain access to my list of points from my drawing area it just doesn't find any containing data. This is my code until now:

paintwidget.cpp (my main window):


PaintWidget::PaintWidget(QWidget parent) :
        QWidget(parent),
        ui(new Ui::PaintWidget)
{
    area = new RenderArea(this);
    ui->setupUi(this);
    connect(ui->displayWidget, SIGNAL(listUpdated(QList)), ui->pointsListWidget,
            SLOT(onListUpdated(QList*)));
    connect(ui->deletePoints, SIGNAL(clicked()), this, SLOT(deleteItem()));
}
void PaintWidget::deleteItem()
{
    area->deletePoint(ui->pointsListWidget->currentItem());
}

renderarea.cpp (my drawing area):


void RenderArea::mousePressEvent(QMouseEvent *e)
{
    point = e->pos();
    updateList(point);
    this->update();
}
void RenderArea::updateList(const QPoint& p)
{
    list.append(p);
    if (list.count()>1)
        lineAdded(p);
    emit listUpdated(&list);
}
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]);
}
void RenderArea::deletePoint(QListWidgetItem *item)
{
    bool ok1;
    bool ok2;
    int index = item->text().indexOf(",");
    int x = item->text().left(index).toInt(&ok1, 10);
    int y = item->text().mid(index + 1).toInt(&ok2, 10);
    for (int i = 0; i < list.size(); ++i)
        //find the point with x and y as coordinates and delete it
}

listbox.cpp:


void ListBox::onListUpdated(QList *list)
{
    clear();
    for (int i = 0; i < list->size(); ++i)
        addItem(new QListWidgetItem(QString::number(list->at(i).x()) + ", " +
                                    QString::number(list->at(i).y())));
}

The list from the render area is a QList of QPoints. The problem is that in the FOR-loop the size of the list is 0 so I cannot see any of the points that it should contain. I think that I am failing to initialize it somewhere but I am not sure where.

The points are drawn with QPainter so when I delete the point from the list is there any possibility to delete them from my drawing area also?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

欲拥i 2024-11-20 17:39:22

我怀疑您出于某种原因有两个 RenderArea 小部件闲置。

您正在连接 ui->displayWidget 的信号,但作用于 area 小部件以进行删除。

您不应该调用 ui->displayWidget->deletePoint 或连接 area 的信号吗?

至于重绘,您应该调用小部件的 update() 方法让它重新绘制自己。

I'm suspecting you've got two RenderArea widgets hanging around for some reason.

You're connecting ui->displayWidget's signal, but acting on the area widget for the delete.

Shouldn't you be calling ui->displayWidget->deletePoint or connecting area's signal?

As for the repaint, you should call the widget's update() method to have it repaint itself.

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