Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFrame Border Does Not Draw
Forum Update on Monday, May 27th 2025

QFrame Border Does Not Draw

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 769 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kendavistoo
    wrote on last edited by
    #1

    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;
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved