如何使用 QPainter 在 QMainWindow 中的一组小部件中的特定小部件上绘制?

How to draw with QPainter on a specific widget from a group of widgets in QMainWindow?

这是我的代码:

#include "mainwindow.h"
#include <QDebug>

#include <QCameraInfo>
#include <QHBoxLayout>
#include <fstream>
#include <assert.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    m_QPushButton_calibration = new QPushButton("Calibrate", this);
    connect(m_QPushButton_calibration, SIGNAL (released()),this, SLOT (handleButton()));

    QList<QCameraInfo> l_QListQCameraInfo_available_cameras = QCameraInfo::availableCameras();

    m_QWidget_viewfinder_holder    = new QWidget;
    m_QWidget_viewfinder_holder->setStyleSheet ("background-color: black");

    m_QCameraViewfinder_viewfinder = new QCameraViewfinder(m_QWidget_viewfinder_holder);

    if (l_QListQCameraInfo_available_cameras.length() >= 2)
    {
        m_QCamera_required_camera = new QCamera (l_QListQCameraInfo_available_cameras[1]);

        m_QCamera_required_camera->setViewfinder(m_QCameraViewfinder_viewfinder);
        m_QCamera_required_camera->start ();
    }

    m_QWidget_central     = new QWidget;
    m_QGridLayout_central = new QGridLayout;

    m_QWidget_central->setLayout (m_QGridLayout_central);

    m_QGridLayout_central->addWidget (m_QPushButton_calibration, 0, 0, 1, 1);
    m_QGridLayout_central->addWidget (m_QWidget_viewfinder_holder, 1, 0, 1, 1);

    this->setCentralWidget (m_QWidget_central);

    m_QCameraViewfinder_viewfinder->show();
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial", 30));
    painter.drawText(rect(), Qt::AlignCenter, "Qt");
}

MainWindow::~MainWindow()
{
    delete m_QPushButton_calibration;
    delete m_QCameraViewfinder_viewfinder;
    delete m_QCamera_required_camera;
    delete m_QGridLayout_central;
    delete m_QWidget_central;
}

void MainWindow::handleButton()
{
    qDebug() << "handleButton";
}

我实际上想在 m_QWidget_viewfinder_holder 小部件上画一条线。

  1. QPaintEvent 函数如何知道我希望它在何处画线?

  2. 我可以在继承自QMainWindow的class中使用QPaintEvent作为成员函数吗?

How to draw with QPainter on a specific widget from a group of widgets in QMainWindow?

您不能从另一个小部件上绘制一个小部件。每个小部件负责在 paintEvent() 函数中绘制自己的表面。


How will that QPaintEvent function know where do I want it to draw line?

首先,请注意 QPaintEvent 是一个 class,而不是一个函数。

现在您可能想谈谈 paintEvent() 功能。 该函数“知道”在哪里绘制,因为它是小部件的一部分并且小部件具有几何形状。

例如,如果我想创建一个 Rectangle 小部件来绘制一个边距为 5px 的矩形,我会这样写:

void Rectangle::paintEvent(QPaintEvent * e)
{
    QRect rectangle(5, 5, width() - 5, height() - 5);

    QPainter painter(this);
    painter.drawRect(rectangle);
}

Can I use QPaintEvent as a member function in a class inherited from QMainWindow?

您可以在继承 QWidget 的任何 class 中重新实现 paintEvent() 成员函数。如果你从一个已经绘制了东西的 class 继承,你需要调用你的父 class 函数。

void MainWindow::paintEvent(QPaintEvent *event)
{
    QMainWindow::paintEvent(event); // Let QMainWindow draw itself
    QPainter painter(this);
    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial", 30));
    painter.drawText(rect(), Qt::AlignCenter, "Qt");
}

但是,请注意您不太可能愿意重新实现 MainWindow 的 painteEvent()。您通常想要做的是向 MainWindow 添加一个子窗口小部件。


I actually wish to draw a line on m_QWidget_viewfinder_holder widget.

创建一个 ViewFinderHolder class 像这样:

class ViewFinderHolder: public QWidget {
    Q_OBJECT
    public:
        explicit ViewFinder(QWidget *parent = 0)
    ...
}

Reimplement the paintEvent() function:


class ViewFinderHolder: public QWidget {
    Q_OBJECT
    public:
        explicit ViewFinderHolder(QWidget *parent = 0)
    ...
    protected:
        void paintEvent(QPaintEvent *e);
}

void ViewFinderHolder::paintEvent(QPaintEvent *event)
{
    QLineF line(10.0, 80.0, 90.0, 20.0);

    QPainter(this);
    painter.drawLine(line);
}

最后在MainWindow构造函数中替换:

m_QWidget_viewfinder_holder    = new QWidget;

作者:

m_QWidget_viewfinder_holder    = new ViewFinder();

但是,由于 m_QCameraViewfinder_viewfinderm_QWidget_viewfinder_holder 的子项,它将被绘制在它上面并且可能隐藏您在 ViewFinderHolder::paintEvent() 中所做的绘制。


附带说明一下,您可以删除 MainWindow 的析构函数中的 delete 语句。删除 MainWidow 的实例将删除其子窗口小部件。