Qt绘图不删除元件

时间:2018-07-24 12:04:02

标签: c++ qt

我正在使用Qt 5.11.1和Qt Creator创建一个项目。我的代码在我已重写的paintEvent函数中绘制了多个椭圆。但是由于paintEvent函数的工作方式,我在省略号下的按钮被删除了。我想要一个窗口,该窗口的顶部为椭圆形,底部为按钮。大致看起来像这样:

enter image description here

有没有办法做到这一点。现在,按钮已被删除,我只有省略号。如果有人可以引导我,我将非常高兴。

谢谢。

注意:我的椭圆为绿色,背景为黑色,但是我尝试通过将背景更改为白色或更改按钮的样式表来解决问题,

这是我的.h文件:

#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文件:

#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();
}

这就是我得到的:

enter image description here

我的ui文件是这样的:

enter image description here

1 个答案:

答案 0 :(得分:0)

通常-不要使用QMainWindows类。它适用于具有菜单,状态栏等的“大型”桌面应用程序。您所需要做的就是从QDialog甚至只是QWidget派生。而且您可以自己绘制背景,因此无需弄乱调色板。下面应该工作的最小示例。如果没有显示Bar按钮,则您对目标的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"