首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不擦除组件的Qt绘图

不擦除组件的Qt绘图
EN

Stack Overflow用户
提问于 2018-07-24 20:04:02
回答 1查看 96关注 0票数 0

我正在使用Qt 5.11.1和Qt Creator创建一个项目。我的代码在我覆盖的paintEvent函数中绘制了几个省略号。但是由于paintEvent函数的工作方式,我在省略号下面的按钮正在被擦除。我想有一个窗口,有椭圆在顶部和按钮在窗口的底部。它大致看起来是这样的:

有没有办法做到这一点。现在,按钮正在被删除,而我只有省略号。如果有人能指导我,我会很高兴的。

提前谢谢。

注:我的椭圆是绿色的,背景是黑色的,但我尝试过将背景改为白色或更改按钮的样式表,但都不起作用。

这是我的.h文件:

代码语言:javascript
复制
#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

这是我的.cpp文件:

代码语言:javascript
复制
#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();
}

这是我得到的:

我的ui文件如下所示:

EN

回答 1

Stack Overflow用户

发布于 2018-07-25 04:04:31

一般来说,不要使用QMainWindows类。它适用于有菜单、状态栏等的“大”桌面应用程序,你所需要的就是从QDialog或者甚至是QWidget派生出来。你可以自己画背景,所以不需要弄乱调色板。下面是应该可以工作的最小示例。如果Bar按钮没有出现,那么目标的Qt就会中断:默认的平台样式不起作用。

代码语言:javascript
复制
// 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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51498155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档