代码之家  ›  专栏  ›  技术社区  ›  BUY

不删除组件的Qt绘图

  •  0
  • BUY  · 技术社区  · 6 年前

    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

    我的用户界面文件如下:

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  0
  •   Kuba hasn't forgotten Monica    6 年前

    一般来说-不要使用 QMainWindows QDialog 甚至只是 QWidget . 你可以自己画背景,所以不需要弄乱托盘。下面是最简单的例子。如果 Bar

    // 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"