[英]How to draw with QPainter on a specific widget from a group of widgets in QMainWindow?
This is my code: 这是我的代码:
#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";
}
I actually wish to draw a line on m_QWidget_viewfinder_holder
widget. 我实际上希望在
m_QWidget_viewfinder_holder
小部件上画一条线。
How will that QPaintEvent
function know where do I want it to draw line? QPaintEvent
函数如何知道我要在哪里画线?
Can I use QPaintEvent
as a member function in a class inherited from QMainWindow
? 我可以在从
QMainWindow
继承的类QPaintEvent
用作成员函数吗?
How to draw with QPainter on a specific widget from a group of widgets in QMainWindow?
如何使用QPainter在QMainWindow中的一组小部件上的特定小部件上绘制?
You cannot draw on a widget from another widget. 您不能从另一个窗口小部件上绘制。 Each widget is responsible for drawing its own surface in the
paintEvent()
function. 每个小部件负责在
paintEvent()
函数中绘制其自己的表面。
How will that QPaintEvent function know where do I want it to draw line?
QPaintEvent函数如何知道我要在哪里画线?
First, note that QPaintEvent
is a class, not a function. 首先,请注意
QPaintEvent
是一个类,而不是一个函数。
Now you probably want to talk about the paintEvent()
function. 现在您可能想谈谈
paintEvent()
函数。 The function "knows" where to draw because it is part of a widget and the widget has a geometry. 该函数“知道”在哪里绘制,因为它是窗口小部件的一部分并且窗口小部件具有几何形状。
For instance if I want to create a Rectangle
widget that draws a rectangle with a 5px margin, I would write something like: 例如,如果我想创建一个
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?
我可以在从QMainWindow继承的类中将QPaintEvent用作成员函数吗?
You can reimplement the paintEvent()
member function in any class that inherits QWidget
. 您可以在继承
QWidget
任何类中重新实现paintEvent()
成员函数。 If you inherits from a class that already draws something you need to call your parent class function. 如果您从已经绘制了东西的类继承,则需要调用父类函数。
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");
}
However, please note that you are not likely willing to reimplement the painteEvent()
of a MainWindow. 但是,请注意,您不太可能会重新实现MainWindow的
painteEvent()
。 What you generally want to do is to add a child widget to the MainWindow. 您通常要做的是将一个子小部件添加到MainWindow中。
I actually wish to draw a line on m_QWidget_viewfinder_holder widget.
我实际上希望在m_QWidget_viewfinder_holder小部件上画一条线。
Create a ViewFinderHolder
class like so: 创建一个
ViewFinderHolder
类,如下所示:
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);
}
Finally in the MainWindow constructor replace: 最后在MainWindow构造函数中替换:
m_QWidget_viewfinder_holder = new QWidget;
by: 通过:
m_QWidget_viewfinder_holder = new ViewFinder();
However, as m_QCameraViewfinder_viewfinder
is a child of m_QWidget_viewfinder_holder
, it will be drawn over it and may hide the drawing you did in ViewFinderHolder::paintEvent()
. 但是,由于
m_QCameraViewfinder_viewfinder
是m_QWidget_viewfinder_holder
,它将被绘制在其上方,并可能隐藏您在ViewFinderHolder::paintEvent()
中ViewFinderHolder::paintEvent()
的绘制。
On a side note, you can remove the delete statements in the destructor of MainWindow. 另外,您可以在MainWindow的析构函数中删除delete语句。 Deleting an instance of MainWidow will delete its child widgets.
删除MainWidow的实例将删除其子窗口小部件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.