How to subclassing QWidget with passing additional user defined class parameter

  • Hi,

    I'm quite new to Qt programming - so I'm not sure if my solution approach is going in the right direction. Also sorry for my bad english.

    I like to build a Qt v5.4 Windows desktop application with menu and dialogboxes and QPainter drawing capability. This should be done the most simple and standard way.

    With Qt Creators v3.3 Project-Wizard I created a QWidgets Windows desktop application with generated Qt forms file. Qt Creator did create Main.cpp, Mainwindow.cpp and Mainwindow.ui files.

    For drawing with QPainter I subclassed QWidget with an additional paintEvent() function. In mainwindows.ui this subclassed QWidget class called "Paint" was assigned with Qt Designer to Mainwindow::centralWidget.

    My drawings in Paint::paintEvent() are working fine.

    Now I like to pass my own config class to paintEvent(), to config my drawing functions.

    For this is it the right way to change the QWidget subclassed "Paint" and add an extra parameter to its constructor for passing my config class? If so, where in the Qt C++ Code do I pass my config class to the new "Paint" constructor with extra config class parameter? Because "Paint" is assigned to QMainWindow::centralWidget in MainWindow.ui and not in the Qt C++ Code?

    How can it be done to get access to my config class in paintEvent() function?

  • You cannot pass it to paintEvent directly. paintEvent is a virtual function that is called by Qt to render your widget when needed. However, you can just add a new member function on your Paint class that you can use to set your configuration. Store it in a member variable in the class, and read it from your paintEvent implementation.

  • Andre, thank you for your reply and your solution approach.

    My Config class instance got its place right at the beginning of the int main() function. Then it reads its members from a config file (into RAM). During runtime its members are only changed if a user changes them via dialogbox transaction. At application end the Config members are stored to config file.

    After its initialisation the Config class passes a Pointer to its instance to many following classes to config their members and functions. All classes are only reading Config class members exept the dialogbox classes which are reading AND writing.

    So the problem in my view is how to pass a pointer from the Config class instance into "Paint" to make it capable to read the Config information and config its members with it. But maybe there's a way better solution for this problem I'm at the moment not aware of.

  • I think you'll have more luck getting yourself a basic C++ book.

  • Andre, I'm programming C++ for years but not on a high level nor daily.

    Is this the solution you meant:
    // Paint.h
    #include "QWidget.h"
    #include "QPainter.h"
    #include "QPaintEvent.h"
    #include "Config.h"

    class Paint : public QWidget
    Paint(QWidget * parent = 0);

    Config Conf;
    void ReadConfig();

    void paintEvent(QPaintEvent *);

    // Paint.cpp
    #include "paint.h"

    Paint::Paint(QWidget * parent)

    setStyleSheet("background-color: white;");


    void Paint::ReadConfig()
    Conf = ReadConf_FromFile();

    void Paint::paintEvent(QPaintEvent *)
    QPainter p(this);

    painter.drawRect(Conf.X1, Conf.Y1, Conf.X2, Conf.Y2);


    With this solution I only can see getting access to the "global" Config members in reading from Conf file. But the Conf members should be read from RAM. Because "Paint" is not the only class reading Config members.

  • Is there any way to get a Pointer to the QWidget subclassed centralWidget "Paint" and its members and member functions?
    For example via QMainWindow QWidget * centralWidget() const ?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.