简体   繁体   English

使用 QPainter 在 QWidgets 之间画一条线

[英]Using QPainter to draw a line between QWidgets

I'm working on an application where I need to be able to draw a line between two QWidget objects.我正在开发一个应用程序,我需要能够在两个QWidget对象之间画一条线。 I have tried quite a few things, but my current attempt (which I think is in the right direction I just think I'm missing something) is to have the containing widget (which I called DrawWidget and which holds the QGridLayout that the QWidget objects are added to) override the paintEvent method and call the QPainter::drawLine() function.我已经尝试了很多东西,但我目前的尝试(我认为这是正确的方向,我只是认为我错过了一些东西)是拥有包含小部件(我称之为DrawWidget并且它包含QWidget对象的QGridLayout添加到)覆盖paintEvent方法并调用QPainter::drawLine()函数。

The issues I'm having are that:我遇到的问题是:

  1. No matter how I try to get the position of the widgets, the endpoints of the line are always in the wrong place无论我如何尝试获取小部件的位置,线的端点总是在错误的位置
  2. Whenever I try to draw a second line, the first line that I drew gets erased.每当我尝试绘制第二条线时,我绘制的第一条线都会被擦除。

Here is the paintEvent function of the containing widget:这是包含小部件的paintEvent函数:

void paintEvent(QPaintEvent *)
{
    if (!drewSinceUpdate){
        drewSinceUpdate = true;
        QPainter painter(this);
        painter.setPen(QPen(Qt::black));

        painter.drawLine(start->geometry().center(), end->geometry().center());
    }
}

I have tried many different ways to get the correct position of the widgets in the last line of paintEvent , which I will post some of the ways (I can't remember all of them):我已经尝试了很多不同的方法来在paintEvent的最后一行中获得小部件的正确位置,我将发布一些方法(我不记得所有方法了):

painter.drawLine(start->pos(), end->pos());
painter.drawLine(start->mapToGlobal(start->geometry().center()), end->mapToGlobal(end->geometry().center()));
painter.drawLine(this->mapToGlobal(start->geometry().center()), this->mapToGlobal(end->geometry().center()));
painter.drawLine(start->mapTo(this, start->pos()), end->mapTo(this, end->pos()));
painter.drawLine(this->mapFrom(start, start->pos()), this->mapFrom(end, end->pos()));

And just to make my question clear, here is an example of what I am looking for, taken from QT Diagram Scene Example :只是为了让我的问题清楚,这是我正在寻找的一个例子,取自QT Diagram Scene Example 我想要的例子 But this is what I end up getting:但这就是我最终得到的:

我得到的 Thank you for any help you can provide.感谢您提供的任何帮助。

NOTE:注意:

- start and end are both QWidget objects which I passed in using another method - startend都是我使用另一种方法传入的QWidget对象

-The hierarchy relevant to DrawWidget is: - 与DrawWidget相关的层次结构是:

QMainWindow
->QScrollArea
  ->DrawWidget
    ->QGridLayout
      ->Items       <-- These are the things I want to connect

EDIT: To make a Complete and Verifiable example, here is the entirety of the relevant code.编辑:要制作一个完整且可验证的示例,这里是相关代码的全部内容。

MainWindow.cpp:主窗口.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QScrollBar>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    // Setting up the relevant hierarchy 
    ui->setupUi(this);
    scrollArea = new QScrollArea();
    setCentralWidget(scrollArea);

    drawWidget = new DrawWidget();
    gridLayout = new QGridLayout();
    gridLayout->setSpacing(300);
    drawWidget->setLayout(gridLayout);

    scrollArea->setWidget(drawWidget);
    scrollArea->setWidgetResizable(true);

    AddItemSlot();

    QApplication::connect(scrollArea->horizontalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(scrollHorizontal()));
}

// This is just creating a single one of the example widgets which I want to connect
QWidget* MainWindow::CreateNewItem(){
    QWidget* itemWidget = new QWidget();
    itemWidget->setStyleSheet("background-color: lightgray");
    QHBoxLayout* singleItemLayout = new QHBoxLayout();
    itemWidget->setLayout(singleItemLayout);

    QTextEdit* textEdit = new QTextEdit(std::to_string(counter++).c_str());
    textEdit->setStyleSheet("background-color:white;");
    singleItemLayout->addWidget(textEdit);

    QVBoxLayout* rightSidePanel = new QVBoxLayout();
    rightSidePanel->setAlignment(Qt::AlignTop);

    QPushButton* button1 = new QPushButton("Top Button");

    QApplication::connect(button1, SIGNAL(clicked(bool)), this, SLOT(AddItemSlot()));

    rightSidePanel->addWidget(button1);

    QWidget* rightPanelWidget = new QWidget();
    rightSidePanel->setMargin(0);
    rightPanelWidget->setLayout(rightSidePanel);

    singleItemLayout->addWidget(rightPanelWidget);

    itemWidget->setLayout(singleItemLayout);
    itemWidget->setMinimumWidth(400);
    itemWidget->setFixedSize(400,200);

    return itemWidget;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::scrollHorizontal()
{
    scrollArea->ensureWidgetVisible(noteItems.back());
}

void MainWindow::AddItemSlot()
{
    QWidget* w = CreateNewItem();
    gridLayout->addWidget(w,currRow, currCol++);
    if (!noteItems.empty()){
        drawWidget->updateEndpoints(noteItems.back(), w);
    }
    noteItems.push_back(w);
}

MainWindow.h主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QGridLayout>
#include <QWidget>
#include <QMainWindow>
#include <QScrollArea>
#include <drawwidget.h>
#include "drawscrollarea.h"
#include <vector>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void scrollHorizontal();
    void AddItemSlot();

private:
    Ui::MainWindow *ui;
    QWidget* CreateNewItem();
    int counter = 0, currCol = 0, currRow = 0;
    std::vector<QWidget*> noteItems;

    QScrollArea* scrollArea;
    DrawWidget* drawWidget;
    QGridLayout* gridLayout;
};

#endif // MAINWINDOW_H

DrawWidget.cpp: DrawWidget.cpp:

#include "drawwidget.h"
#include <QDebug>
#include <QRect>

DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{

}

void DrawWidget::paintEvent(QPaintEvent *)
{
    if (!drewSinceUpdate){
        drewSinceUpdate = true;
        QPainter painter(this);
        painter.setPen(QPen(Qt::black));

        for (ConnectedPair pair : items){
            const QWidget* from = pair.from;
            const QWidget* to =pair.to;

            QPoint start =  from->mapToGlobal(from->rect().topRight() +  QPoint(0, from->height()/2));
            QPoint end = to->mapToGlobal(to->rect().topLeft() +  QPoint(0, to->height()/2));

            painter.drawLine(mapFromGlobal(start), mapFromGlobal(end));
        }
    }
}

void DrawWidget::updateEndpoints(QWidget* startIn, QWidget* endIn){
    drewSinceUpdate = false;
    items.push_back(ConnectedPair{startIn, endIn});
}

DrawWidget.h绘图控件.h

#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H

#include <QWidget>
#include <QPainter>
#include <QtCore>
#include <vector>

class DrawWidget : public QWidget
{
    Q_OBJECT
public:
    explicit DrawWidget(QWidget *parent = nullptr);
    void updateEndpoints(QWidget* startIn, QWidget* endIn);
    virtual void paintEvent(QPaintEvent *);
signals:

private:
    struct ConnectedPair {
        const QWidget* from;
        const QWidget* to;
    };

    std::vector<ConnectedPair> items;
    bool drewSinceUpdate = true;
};

#endif // DRAWWIDGET_H

For this case we use the function mapToGlobal() and mapfromGlobal() , since pos() returns a position with respect to the parent and this can cause problems if the widget has different parents.对于这种情况,我们使用函数mapToGlobal()mapfromGlobal() ,因为 pos() 返回相对于父级的位置,如果小部件具有不同的父级,这可能会导致问题。

drawwidget.h drawwidget.h

#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H

#include <QWidget>

class DrawWidget : public QWidget
{
    Q_OBJECT
public:
    explicit DrawWidget(QWidget *parent = nullptr);

    void addWidgets(const QWidget *from, const QWidget *to);

protected:
    void paintEvent(QPaintEvent *);

private:
    struct WidgetsConnected {
        const QWidget* from;
        const QWidget* to;
    };

    QList<WidgetsConnected> list;

};

#endif // DRAWWIDGET_H

drawwidget.cpp drawwidget.cpp

#include "drawwidget.h"

#include <QPainter>
DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{
}

void DrawWidget::addWidgets(const QWidget * from, const QWidget * to)
{
    list.append(WidgetsConnected{from , to});
    update();
}

void DrawWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    for(const WidgetsConnected el: list){
        const QWidget* from = el.from;
        const QWidget* to = el.to;

        QPoint start =  from->mapToGlobal(from->rect().topRight() +  QPoint(0, from->height()/2));
        QPoint end = to->mapToGlobal(to->rect().topLeft() +  QPoint(0, to->height()/2));

        painter.drawLine(mapFromGlobal(start), mapFromGlobal(end));
    }
}

The complete example can be found here .完整的示例可以在这里找到。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM