C++ 不删除元件的Qt绘图
我正在使用Qt5.11.1和QtCreator创建一个项目。我的代码在已重写的paintEvent函数中绘制了几个省略号。但是由于paintEvent函数的工作方式,我在省略号下面的按钮正在被删除。我想要一个顶部有椭圆,底部有按钮的窗口。大致看起来是这样的: 有没有办法做到这一点。现在,按钮正在被删除,我只有椭圆。如果有人能指引我,我会非常高兴 提前谢谢 注意:我的椭圆是绿色的,背景是黑色的,但我尝试过将背景更改为白色或更改按钮的样式表,但没有成功。 这是我的.h文件:C++ 不删除元件的Qt绘图,c++,qt,C++,Qt,我正在使用Qt5.11.1和QtCreator创建一个项目。我的代码在已重写的paintEvent函数中绘制了几个省略号。但是由于paintEvent函数的工作方式,我在省略号下面的按钮正在被删除。我想要一个顶部有椭圆,底部有按钮的窗口。大致看起来是这样的: 有没有办法做到这一点。现在,按钮正在被删除,我只有椭圆。如果有人能指引我,我会非常高兴 提前谢谢 注意:我的椭圆是绿色的,背景是黑色的,但我尝试过将背景更改为白色或更改按钮的样式表,但没有成功。 这是我的.h文件: #ifndef MAI
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
protected:
void paintEvent(QPaintEvent *e);
void setBackGroundColorToBlack();
};
#endif // MAINWINDOW_H
\ifndef主窗口
#定义主窗口
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
私人:
Ui::MainWindow*Ui;
受保护的:
无效油漆事件(QPaintEvent*e);
void setBackGroundColorToBlack();
};
#endif//main窗口
这是我的.cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setBackGroundColorToBlack();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event) {
setUpdatesEnabled(false);
QPainter painterObj;
painterObj.begin(this);
painterObj.setPen(QPen(Qt::green, 2, Qt::SolidLine, Qt::RoundCap));
painterObj.drawEllipse(0, 0, 318, 390);//456
painterObj.drawEllipse(53, 65, 212, 260);//304
painterObj.drawEllipse(106, 130, 106, 130);//152
painterObj.end();
}
void MainWindow::setBackGroundColorToBlack() {
QPalette pal = palette();
// set black background
pal.setColor(QPalette::Background, Qt::black);
this->setAutoFillBackground(true); // This enables the qt to fill the background before the paint event.
this->setPalette(pal);
//update();
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
setBackGroundColorToBlack();
}
MainWindow::~MainWindow()
{
删除用户界面;
}
void主窗口::paintEvent(QPaintEvent*事件){
setUpdatesEnabled(假);
QPainter painterObj;
开始(这个);
setPen(QPen(Qt::green,2,Qt::SolidLine,Qt::RoundCap));
painterObj.drawerlipse(0,0318390);/456
画家b.佩雷利普斯(53,65,212,260);/304
画家b.佩雷利普斯(106130106130);/152
painterObj.end();
}
void主窗口::setBackGroundColorToBlack(){
QPalette pal=调色板();
//设置黑色背景
pal.setColor(Qpalete::Background,Qt::black);
this->setAutoFillBackground(true);//这使qt能够在绘制事件之前填充背景。
这->设置调色板(pal);
//更新();
}
这就是我得到的:
我的ui文件如下所示:
一般-不要使用
QMainWindows
类。它适用于具有菜单、状态栏等的“大型”桌面应用程序。您只需从QDialog
甚至QWidget
派生即可。你可以自己画背景,所以不需要弄乱托盘。下面是最简单的例子。如果栏
按钮未显示,则目标的Qt已中断:默认平台样式不起作用
// https://github.com/KubaO/stackoverflown/tree/master/questions/painted-with-children-51498155
// This project is compatible with Qt 4 and Qt 5
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
QRectF scaled(const QRectF &rect, qreal scale) {
auto const center = rect.center();
auto const w = rect.width()*scale;
auto const h = rect.height()*scale;
return {center.x() - w/2.0, center.y() - h/2.0, w, h};
}
QRectF marginAdded(const QRectF &rect, qreal margin) {
return rect.adjusted(margin, margin, -margin, -margin);
}
const char buttonQSS[] =
"* { background-color: white; border-width: 2px; border-style:solid; border-color: red;"
" border-radius: 3px; padding: 3px; }"
"*:pressed { padding-left: 5px; padding-top: 5px; background-color: lightGray; }";
class MyWindow : public QWidget {
Q_OBJECT
QPushButton m_restoreButton{"Restore Size"};
QPushButton m_otherButton{"Bar"};
QGridLayout m_layout{this};
Q_SLOT void onRestore() { resize(sizeHint()); }
public:
explicit MyWindow(QWidget *parent = {}) : QWidget(parent) {
setAttribute(Qt::WA_OpaquePaintEvent);
m_layout.addItem(new QSpacerItem(0, 100, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 0, 0);
m_layout.addWidget(&m_restoreButton, 1, 0);
m_layout.addWidget(&m_otherButton, 1, 1);
m_restoreButton.setStyleSheet(buttonQSS);
connect(&m_restoreButton, SIGNAL(clicked(bool)), SLOT(onRestore()));
}
protected:
void paintEvent(QPaintEvent *) override {
qreal const penWidth = 2.0;
// Cover the area above all the children
QRectF const area = marginAdded(QRect(0, 0, width(), childrenRect().top() - 10), penWidth);
QPainter p(this);
p.fillRect(rect(), Qt::black);
p.setPen({Qt::green, penWidth});
p.drawEllipse(scaled(area, 3./3.));
p.drawEllipse(scaled(area, 2./3.));
p.drawEllipse(scaled(area, 1./3.));
}
QSize sizeHint() const override { return {320, 568}; /* iPhone 5 */ }
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWindow w;
w.show();
return a.exec();
}
#include "main.moc"
//https://github.com/KubaO/stackoverflown/tree/master/questions/painted-with-children-51498155
//此项目与Qt4和Qt5兼容
#包括
#如果QT\u版本>=QT\u版本检查(5,0,0)
#包括
#恩迪夫
QRectF标度(常量QRectF&rect,qreal标度){
auto const center=rect.center();
自动常数w=矩形宽度()*比例;
自动常数h=垂直高度()*比例;
返回{center.x()-w/2.0,center.y()-h/2.0,w,h};
}
保留边缘(常量和矩形,保留边缘){
调整后的收益率(保证金,保证金,-保证金,-保证金);
}
const char buttonQSS[]=
“*{背景色:白色;边框宽度:2px;边框样式:纯色;边框颜色:红色;”
“边框半径:3px;填充:3px;}”
“*:按下{左侧填充:5px;顶部填充:5px;背景色:浅灰色;}”;
类MyWindow:公共QWidget{
Q_对象
QPushButton m_restoreButton{“Restore Size”};
QPushButton m_otherButton{“Bar”};
QGridLayout m_布局{this};
Q_插槽void onRestore(){resize(sizeHint());}
公众:
显式MyWindow(QWidget*parent={}):QWidget(parent){
setAttribute(Qt::WA_OpaquePaintEvent);
m_layout.addItem(新的QSpacerItem(0,100,QSizePolicy::Minimum,QSizePolicy::MinimumExpanding),0,0);
m_layout.addWidget(&m_restoreButton,1,0);
m_layout.addWidget(&m_otherButton,1,1);
m_restoreButton.setStyleSheet(按钮QSS);
连接(&m_restoreButton)、信号(单击(bool))、插槽(onRestore());
}
受保护的:
无效paintEvent(QPaintEvent*)覆盖{
qreal const penWidth=2.0;
//覆盖所有儿童上方的区域
QRectF const area=marginaded(QRect(0,0,width(),childrenRect().top()-10),penWidth);
油漆工p(本);
p、 fillRect(rect(),Qt::black);
p、 setPen({Qt::green,penWidth});
p、 抽屉(按比例(面积,3/3.);
p、 抽屉直径(按比例(面积,2/3.);
p、 抽屉直径(按比例(面积,1/3.);
}
QSize sizeHint()常量重写{return{320568};/*iPhone 5*/}
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
我的窗口w;
w、 show();
返回a.exec();
}
#包括“main.moc”
提供代码,我想看看哪些部分可以修改,你也说图像被删除了,这很正常,但我会在哪个事件之前更改图像。@eyllanesc最大椭圆下的黑屏部分是我为按钮安排的区域。@eyllanesc我用桌面Qt 5.11.1 MinGW 32位工具包和Android for armeabi-v7a工具包都试过了。它都给出了相同的结果。removesetUpdatesEnabled(false)
通过设置setUpdatesEnabled(false),在父对象之后绘制子对象代码>表示未绘制子对象,这就是此问题产生的原因。在实际设备上测试时,模拟器有许多问题。