当前位置: 首页 > news >正文

潍坊网站建设科技有限公司b站网站推广

潍坊网站建设科技有限公司,b站网站推广,网页设计与制作教程书,动态站 网站地图怎么做qt窗体布局 窗体渲染过程 qt中窗体渲染逻辑顺序为 本窗体->子窗体/控件 递归,也就是说先渲染父窗体再渲染子窗体。其中子窗体按加入时的先后顺序进行渲染。通过下方的函数调用堆栈可以看出窗体都是在widget组件源码的widgetprivate::drawwidget中进行渲染的&am…

qt窗体布局 

窗体渲染过程

qt中窗体渲染逻辑顺序为 本窗体->子窗体/控件 递归,也就是说先渲染父窗体再渲染子窗体。其中子窗体按加入时的先后顺序进行渲染。通过下方的函数调用堆栈可以看出窗体都是在widget组件源码的widgetprivate::drawwidget中进行渲染的,qt内部完成对当前窗体的渲染后再drawWidget最后调用paintSiblingsRecursive这个递归调用函数,对所有子窗体进行逐个渲染,paintEvent也是在这个paintSiblingsRecursive之前就完成了

qt窗体的一次渲染过程分两部分工作,第一部分工作是qt内部对窗体进行渲染工作(这个渲染过程对编程人员来说是不可见不可直接干预的,只能通过设置style stylesheet palette进行事先设置),第二部分是paintEvent中,编程人员可以在这个里面通过Qpainter进行渲染发挥,用QPainter渲染出来的结果去覆盖qt内部渲染出来的结果。其中window、widget、form都是在paintEvent之外渲染的,而button、label等等控件则是在paintEvent之中进行(隐藏在QPushButton::paintEvent中)。 

qt的组件被触发渲染/绘制时,QPainter就需要重新进行渲染/绘制,否则QPainter的内容就会被覆盖,所以如果QPainter需要对窗体进行渲染操作,需要在目标窗体中的paintEvent中去实现QPainter的操作步骤。


demo中窗体结构
MainWindow
    centralWidget
        pushbutton
        vboxlayout
            mywidget
                vboxlayout
                    mybutton
        tbutton1
        tbutton2
效果

mybutton.cppMyButton::MyButton(QWidget* parent):QPushButton(parent) {setAttribute(Qt::WA_StyledBackground);setStyleSheet("background-color:rgba(255,255,0,100)"); //设置mybutton背景色为黄色半透明(半透明叠加好像无效)
}---------------------
mywidget.cppMyWidget::MyWidget(QWidget *parent): QWidget{parent}
{QVBoxLayout *layout =new QVBoxLayout(this);setLayout(layout);m_button = new MyButton(this);m_button->setText("button_myWidget");layout->addWidget(m_button);layout->setAlignment(m_button,Qt::AlignCenter);//resize(400, 400);//layout会自动设置窗体的size。setAttribute(Qt::WA_StyledBackground);setStyleSheet("background-color: rgba(255,0,0,100)");  //设置mywidget窗体背景色位半透明
}--------------
mainwindow.cppMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);QPushButton *button1 =new QPushButton(this->centralWidget());//这里就表示已经将pushbutton加入到centralwidget中了button1->setGeometry(QRect(270, 20, 200, 100));button1->setText("tbutton1");layout = new QVBoxLayout(this->centralWidget());m_widget = new MyWidget(this->centralWidget());layout->addWidget(m_widget);this->centralWidget()->setLayout(layout);QPushButton *button =new QPushButton(this->centralWidget());button->setGeometry(QRect(500, 20, 200, 100));button->setText("tbutton2");
}


qt中widget、window、form的渲染过程对开发者来说是隐藏的。但是qt为开发者提供了QPainter进行画面渲染(绘制)工作的类,qpainter就相当于opengl中的render,可以自由操控渲染过程以及渲染目标,是比较方便灵活的。

它的渲染过程由qpainter::begin(paintdevice)开始到qpainter::end()结束,渲染过程是独立的,不受qt内部渲染窗体的过程的影响。其中当painter构造函数中含有paintdevice参数时,在构造函数中就会调用begin(),不需要再显式调用begin();painter对象在析构的时候会自动调用end(),可以不需要显式调用end()。

qt的窗体渲染与QPainter渲染冲突问题

qt内部对窗体渲染也是直接渲染到QWidget的QPaintDevice的内存上,QPainter也是放在QWidget的QPaintDevice内存上。这样就会导致渲染结果的冲突。QPainter对窗体的渲染需要放在paintEvent中,因为paintEvent是在qt内部对widget渲染完了后再执行的,这样用户用QPainter自定义的渲染结果才能在qt内部渲染结果上叠加。而不是被qt内部渲染结果冲刷掉!!!

QPainter渲染无效原因

需要注意的是QPainter应该在渲染目标对象的PaintEvent中进行工作,否则渲染的结果会被冲刷掉。比如 在Mainwindow中的paintEvent中用QPainter对mainWindow的子窗体centralwidget进行渲染,就不会有任何效果,因为centralwidget这个子窗体的渲染是在Mainwindow的paintevent完成之后才进行的。可以将覆盖在centralwideget上的mywidget作为渲染目标,在mywidget中的paintEvent中完成QPainter的工作。
qt 快捷功能 快速生成代码 父类虚函数重写 查看父类及父类中的虚函数

QPainter使用 一定要设置绘制对象,绘制对象必须继承自QPaintDevice。一般用在paintEvent()函数中

​​​​​​​void MyWidget::paintEvent(QPaintEvent *event)
{QPainter painter(this);   //传入this表示要在mywidget上进行绘制painter.drawImage(this->rect(),QImage("G:/1/1.png"));
}
    QPainter painter();//painter->begin(0); // impossible - paint device cannot be 0 when it begin painting//QPixmap image(0, 0);//painter->begin(&image); // impossible - image.isNull() == true;painter->begin(myWidget);//painter->begin(myWidget); // impossible - only one painter at a timepainter.drawEllipse(QRectF(0,0,100,100));painter.end();

下面四个类都继承自QPaintDevice,都可以让QPainter进行绘制

QPixmap专门为图像在屏幕上的显示做了优化
QBitmap是QPixmap的一个子类,它的色深限定为1,可以使用 QPixmap的isQBitmap()函数来确定这个QPixmap是不是一个QBitmap,因为QBitmap色深小,因此只占用很少的存储空间,所以适合做光标文件和笔刷。
QImage专门为图像的像素级访问做了优化
QPicture则可以记录和重现QPainter的各条命令。 QPicture将QPainter的命令序列化到一个IO设备,保存为一个平台独立的文件格式

对上面的demo做个小调整,将mywidget中的setstylesheet设置到centralwidget中,并在mywidget的paintevent中绘制一块不透明的区域

mybutton.cppMyButton::MyButton(QWidget* parent):QPushButton(parent) {setAttribute(Qt::WA_StyledBackground);setStyleSheet("background-color:rgba(0,0,255,100)"); //设置mybutton背景色为黄色半透明(半透明叠加好像无效)
}---------------------
mywidget.cppMyWidget::MyWidget(QWidget *parent): QWidget{parent}
{QVBoxLayout *layout =new QVBoxLayout(this);setLayout(layout);m_button = new MyButton(this);m_button->setText("button_myWidget");layout->addWidget(m_button);layout->setAlignment(m_button,Qt::AlignCenter);//resize(400, 400);//layout会自动设置窗体的size。//setAttribute(Qt::WA_StyledBackground);//setStyleSheet("background-color: rgba(77,66,105,100)");  //设置mywidget窗体背景色位半透明
}void MyWidget::paintEvent(QPaintEvent *event)
{QPainter painter(this);QRect ret = this->rect();painter.fillRect(QRect(ret.x()+50,ret.y()+50,ret.width()*3/2,ret.height()/2), QColor(0,255,0,100));QWidget::paintEvent(event);
}--------------
mainwindow.cppMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);this->centralWidget()->setAttribute(Qt::WA_StyledBackground);this->centralWidget()->setStyleSheet("background-color: rgba(255,0,0,100)"); QPushButton *button1 =new QPushButton(this->centralWidget());//这里就表示已经将pushbutton加入到centralwidget中了button1->setGeometry(QRect(270, 20, 200, 100));button1->setText("tbutton1");layout = new QVBoxLayout(this->centralWidget());m_widget = new MyWidget(this->centralWidget());layout->addWidget(m_widget);this->centralWidget()->setLayout(layout);QPushButton *button =new QPushButton(this->centralWidget());button->setGeometry(QRect(500, 20, 200, 100));button->setText("tbutton2");
}

 效果

centralwidget的子窗体及其递归都继承了他的stylesheet(mybutton有额外设置的stylesheet,所以没有继承),而mywidget的Qpainter绘制的区域并不受mywidget继承的stylesheet而渲染出的结果所影响。

qt QLabel QPushButton 控件重写paintEvent后 控件消失-CSDN博客   

qt 6.7 在布局中的按和文本框,用resize设置大小,无效_layer resize 设置无效-CSDN博客 


文章转载自:
http://party.c7629.cn
http://saltwater.c7629.cn
http://outrank.c7629.cn
http://haneda.c7629.cn
http://codpiece.c7629.cn
http://adamic.c7629.cn
http://allah.c7629.cn
http://diathermize.c7629.cn
http://jonnock.c7629.cn
http://meteorous.c7629.cn
http://clarino.c7629.cn
http://wardship.c7629.cn
http://coffer.c7629.cn
http://nonesuch.c7629.cn
http://monologize.c7629.cn
http://dimissory.c7629.cn
http://kistna.c7629.cn
http://bedtime.c7629.cn
http://ammoniacal.c7629.cn
http://unrhythmic.c7629.cn
http://aerothermoacoustics.c7629.cn
http://barrable.c7629.cn
http://similarity.c7629.cn
http://ecclesiae.c7629.cn
http://lunge.c7629.cn
http://eightsome.c7629.cn
http://strangelove.c7629.cn
http://incapacity.c7629.cn
http://washdown.c7629.cn
http://pneumonic.c7629.cn
http://earning.c7629.cn
http://seggie.c7629.cn
http://sparseness.c7629.cn
http://miscible.c7629.cn
http://doth.c7629.cn
http://inaccessibility.c7629.cn
http://dulcimore.c7629.cn
http://dicker.c7629.cn
http://skeleton.c7629.cn
http://judicator.c7629.cn
http://weeksite.c7629.cn
http://apex.c7629.cn
http://forequarter.c7629.cn
http://moldingplane.c7629.cn
http://endodontic.c7629.cn
http://insensitive.c7629.cn
http://cuttle.c7629.cn
http://calamographer.c7629.cn
http://fallaciously.c7629.cn
http://signatary.c7629.cn
http://dandyism.c7629.cn
http://gambir.c7629.cn
http://fledging.c7629.cn
http://enzootic.c7629.cn
http://eeriness.c7629.cn
http://unlively.c7629.cn
http://acrylate.c7629.cn
http://hermaphrodism.c7629.cn
http://sampan.c7629.cn
http://cutinize.c7629.cn
http://ament.c7629.cn
http://adrenalectomy.c7629.cn
http://gentilism.c7629.cn
http://goatpox.c7629.cn
http://amplifier.c7629.cn
http://repercussive.c7629.cn
http://lymphangiitis.c7629.cn
http://colloquia.c7629.cn
http://overdrove.c7629.cn
http://irishman.c7629.cn
http://hatted.c7629.cn
http://preternatural.c7629.cn
http://hat.c7629.cn
http://mappist.c7629.cn
http://circumambience.c7629.cn
http://hymnal.c7629.cn
http://eyewitness.c7629.cn
http://conclave.c7629.cn
http://vest.c7629.cn
http://frizzly.c7629.cn
http://feather.c7629.cn
http://perivascular.c7629.cn
http://panic.c7629.cn
http://bread.c7629.cn
http://brickdust.c7629.cn
http://personable.c7629.cn
http://lymphocytotic.c7629.cn
http://retry.c7629.cn
http://tightwad.c7629.cn
http://mammal.c7629.cn
http://gangrene.c7629.cn
http://matriarchate.c7629.cn
http://orchardman.c7629.cn
http://warsle.c7629.cn
http://beanball.c7629.cn
http://anticathexis.c7629.cn
http://wrongheaded.c7629.cn
http://fingersmith.c7629.cn
http://deceleration.c7629.cn
http://dtv.c7629.cn
http://www.zhongyajixie.com/news/101103.html

相关文章:

  • 为网站制定一个推广计划seo文章
  • 网站建设手稿长沙网站定制公司
  • 网站一次性链接怎么做专业恶意点击软件
  • 有没有做游戏评测的网站展示型网站设计公司
  • 用校园网如何搭建WordPressseo薪资seo
  • 免费的网站推广怎么做效果好软文推广营销
  • 有哪些网站可以做外贸免费推广平台排行榜
  • 手机网站建设机构软文投稿平台有哪些
  • 海报设计论文免费的电脑优化软件
  • 工程承包网站哪个好?百度网页网址
  • wordpress在线商城seo咨询服务价格
  • 抖音企业服务平台网站怎么优化seo
  • 网站移动页面怎么做成都抖音seo
  • 个人网页的代码资源企业网站排名优化价格
  • 淘客单网站网络推广公司服务内容
  • 玉林做网站公司信息推广的方式有哪些
  • 企业网站的内容seo排名优化推广报价
  • 公司网站域名续费上海网站制作开发
  • 做ppt介绍网站app开发成本预算表
  • 盐城网站建设哪家好搜索引擎优化学习
  • 网站免费源码大全无用下载真正免费建站
  • 如何做网站建设方案百度推广开户怎么开
  • 什么网站代做毕业设计比较好曹操论坛seo
  • http网站跳转怎么做东莞做网页建站公司
  • 将网站加入小程序去哪里推广软件效果好
  • 哪一个网站有做实验的过程泰州网站排名seo
  • 鞍山晟宇网站建设网络舆情的网站
  • 保定网站搜索排名电脑上突然出现windows优化大师
  • 做经营行网站需要什么培训推广 seo
  • 建筑网站的功能模块有哪些产品推广方案怎么做