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. How to subclassing QWidget with passing additional user defined class parameter
Forum Updated to NodeBB v4.3 + New Features

How to subclassing QWidget with passing additional user defined class parameter

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.9k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mireiner
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mireiner
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mireiner
            wrote on last edited by
            #5

            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
            {
            public:
            Paint(QWidget * parent = 0);

            Config Conf;
            void ReadConfig();
            

            protected:
            void paintEvent(QPaintEvent *);
            };

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

            Paint::Paint(QWidget * parent)
            {
            ReadConfig();

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

            };

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

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

            p.setRenderHint(QPainter::Antialiasing);
            
            painter.setPen(Qt::darkGreen);
            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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mireiner
              wrote on last edited by
              #6

              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 ?

              1 Reply Last reply
              0

              • Login

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