Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

QFrame Border Does Not Draw



  • I am including a simple example which has a MainWindow, and two classes, ClassAFrame and ClassBFrame, which inherit QFrame. The MainWindow establishes an instance of each of the classes. Both have style QFrame::Box | QFrame::Raised with a linewidth of 3. ClassAFrame contains a paintEvent function while ClassBFrame does not. MainWindow draws the frame of ClassBFrame correctly but does not draw the frame of ClassAFrame. What am I doing wrong?

    classaframe.h:

    #ifndef CLASSAFRAME_H
    #define CLASSAFRAME_H
    
    #include "QFrame"
    #include <QPainter>
    
    class ClassAFrame : public QFrame
    {
    public:
        ClassAFrame(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Widget);
    
    protected:
        void paintEvent(QPaintEvent*);
    };
    
    #endif // CLASSAFRAME_H
    

    classbframe.h:

    #ifndef CLASSBFRAME_H
    #define CLASSBFRAME_H
    
    #include <QFrame>
    #include <QPainter>
    
    class ClassBFrame : public QFrame
    {
    public:
        ClassBFrame(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Widget);
    };
    
    #endif // CLASSBFRAME_H
    
    

    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <classaframe.h>
    #include <classbframe.h>
    #include <QPushButton>
    #include <QPainter>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        QPushButton* buttonM;
        QPushButton* buttonA;
    
        ClassAFrame* aFrame;
        ClassBFrame* bFrame;
    
    protected:
        void paintEvent(QPaintEvent*);
    
    public slots:
        void SlotHandleClickMain();
        void SlotHandleClickAFrame();
    };
    
    #endif // MAINWINDOW_H
    
    

    classaframe.cpp:

    #include "classaframe.h"
    
    ClassAFrame::ClassAFrame(QWidget *parent, Qt::WindowFlags f) : QFrame(parent, f)
    {
    }
    
    void ClassAFrame::paintEvent(QPaintEvent*){
        QPainter* painter = new QPainter(this);
        int x = 20 + (int)(50 + (rand() % 20));
        int y = (this->height() - 40);
        QRect myRect(x,y,12,12);
        painter->fillRect(myRect, Qt::blue);
        delete painter;
    }
    
    

    classbframe.cpp:

    #include "classbframe.h"
    
    ClassBFrame::ClassBFrame(QWidget *parent, Qt::WindowFlags f) : QFrame(parent, f)
    {
    }
    
    

    main cpp:

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    

    mainwindow.cpp:

    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        buttonM = new QPushButton("Update Main", this);
        connect(buttonM, &QPushButton::clicked, this, &MainWindow::SlotHandleClickMain);
    
        buttonA = new QPushButton("Update AFrame", this);
        connect(buttonA, &QPushButton::clicked, this, &MainWindow::SlotHandleClickAFrame);
    
        aFrame = new ClassAFrame(this, Qt::Widget);
        bFrame = new ClassBFrame(this, Qt::Widget);
    
        this->setGeometry (10, 100, 500, 500);
        buttonM->setGeometry(30, 20, 120, 24);
        buttonA->setGeometry(230, 20, 120, 24);
    
        aFrame->setGeometry( 30, 130, 300, 100);
        bFrame->setGeometry( 30, 260, 300, 100);
    
        aFrame->setFrameStyle(QFrame::Box | QFrame::Raised);
        bFrame->setFrameStyle(QFrame::Box | QFrame::Raised);
    
        aFrame->setLineWidth(3);
        bFrame->setLineWidth(3);
    
        aFrame->setVisible(true);
        bFrame->setVisible(true);
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::SlotHandleClickMain(){
        this->update();
    }
    
    void MainWindow::SlotHandleClickAFrame(){
        aFrame->update();
    }
    
    void MainWindow::paintEvent(QPaintEvent*){
    
        QPainter* painter = new QPainter(this);
        int x = 20 + (int)(50 + (rand() % 20));
        int y = (this->height() - 40);
        QRect myRect(x,y,12,12);
        painter->fillRect(myRect, Qt::red);
        delete painter;
    }
    
    

  • Lifetime Qt Champion

    Hi,

    You should call the base's class paintEvent and then do your own drawings.

    On a side note, there's no need to allocate a QPainter on the heap, keep on the stack, that will simplify your code.


Log in to reply