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 attach class to qt project without ui file?

how to attach class to qt project without ui file?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 807 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.
  • timob256T Offline
    timob256T Offline
    timob256
    wrote on last edited by
    #1

    created a class which added to the project without ui.
    I can't call it correctly.

    ** the class I'm embedding: **

    #ifndef OKNONASTROEK_H
    #define OKNONASTROEK_H
    
    #include <QMainWindow>
    #include <QDebug>
    #include <QPainter>
    #include <QPen>
    #include <QFont>
    #include <QFontMetrics>
    #include <QKeyEvent>
    #include <QRegion>
    #include <QPointF>
    #include <QGraphicsView>
    
    namespace Widgets {
    struct texnStructur
    {
        texnStructur () // immediately fill it with zeros
        {
            for (uint i = 0; i <sizeof (m_t_razm) / sizeof (m_t_razm [0]); i ++)
                m_t_razm [i] = 0;
        }
        float m_t_razm [12];
    };
    
    
    class window pane: public QWidget
    {
        Q_OBJECT
    
    public:
        windowspar (QWidget * parent = 0);
        ~ windowspawn ();
    
        void razmer_element (int x, int y);
    
    
    protected:
        void paintEvent (QPaintEvent * event);
    
    
        texnStructur _myTexpr;
    
    };
    }
    #endif // OKNONASTROEK_H
    

    ** here is the "body" of the class **

    #include "oknonastroek.h"
    
    Widgets :: windowastroek :: windowastroek (QWidget * parent)
        : QWidget (parent)
    {
    }
    
    Widgets :: windowspawn :: ~ windowpattern ()
    {
    
    }
    
    void Widgets :: oknonastroek :: razmer_element (int x, int y)
    {
        // set the size of the lines here
        _myTexpr.m_t_razm [0] = x / 60.0; // X indent
        _myTexpr.m_t_razm [1] = g / 4.0 + g / 60.0; // Y indent
        _myTexpr.m_t_razm [2] = x / 3.0; // width (window) x
        _myTexpr.m_t_razm [3] = g / 1.4; // height (window) y
    }
    
    void Widgets :: windowastroek :: paintEvent (QPaintEvent * event)
    {
        QPainter painter (this); // Create a renderer object
        QBrush bgfon (QColor (180,220,220), Qt :: SolidPattern); // set the background color and fill type (with background without background and fill type)
        painter.setPen (Qt :: NoPen); // paint brush if not needed
    
        painter.setRenderHint (QPainter :: Antialiasing); // remove sharp cubes
        painter.setBrush (bgfon); // set the background color
        razmer_element (this-> width (), this-> height ()); // set the size of the element
    
        painter.drawRect (_myTexpr.m_t_razm [0], _myTexpr.m_t_razm [1], _myTexpr.m_t_razm [2], _myTexpr.m_t_razm [3]);
    }
    

    ** And here's the main class: **

    #ifndef OKNOWIBORA_H
    #define OKNOWIBORA_H
    
    #include <QMainWindow>
    #include <QDebug>
    #include <QPainter>
    #include <QPen>
    #include <QFont>
    #include <QFontMetrics>
    #include <QKeyEvent>
    #include <QRegion>
    #include <QPointF>
    #include <QGraphicsView>
    
    #include "oknonastroek.h"
    
    
    
    struct texnStructur
    {
        texnStructur () // immediately fill it with zeros
        {
            for (uint i = 0; i <sizeof (m_t_razm) / sizeof (m_t_razm [0]); i ++)
                m_t_razm [i] = 0;
        }
        
        float m_t_razm [12];
    };
    
    WindowVibora class: public QMainWindow
    {
        Q_OBJECT
    
    public:
        WindowWibora (QWidget * parent = 0);
        ~ WindowVibora ();
    
        void razmer_element (int x, int y);
    
    
    protected:
        void paintEvent (QPaintEvent * event);
        Widgets :: windowstrip * oknonastr;
    
        texnStructur _myTexpr;
    };
    
    #endif // OKNOWIBORA_H
    

    ** "body" of the main class **

    #include "oknowibora.h"
    
    OknoWibora :: OknoWibora (QWidget * parent)
        : QMainWindow (parent)
    {
        oknonastr = new Widgets :: oknonastroek (this);
    }
    
    OknoWibora :: ~ OknoWibora ()
    {
    
    }
    
    void OknoWibora :: paintEvent (QPaintEvent * event)
    {
        QPainter painter (this); // Create a renderer object
        QBrush bgfon (QColor (220,180,220), Qt :: SolidPattern); // set the background color and fill type (with background without background and fill type)
    
        QPen pen_abris (Qt :: black, 2, Qt :: SolidLine, Qt :: FlatCap); // outline (compass) brush
        QPen pen_line (Qt :: black, 2, Qt :: SolidLine, Qt :: FlatCap); // brush for lines and text
    
        painter.setRenderHint (QPainter :: Antialiasing); // remove sharp cubes
        painter.setBrush (bgfon); // set the background color
    
        painter.setPen (Qt :: NoPen); // paint brush if not needed
        razmer_element (this-> width (), this-> height ()); // set the dimensions
        painter.drawRect (_myTexpr.m_t_razm [0], _myTexpr.m_t_razm [1], _myTexpr.m_t_razm [2], _myTexpr.m_t_razm [3]);
    
        oknonastr-> update ();
    
        qDebug () << "rere1";
    }
    void OknoWibora :: razmer_element (int x, int y)
    {
        // set the size of the lines here
        _myTexpr.m_t_razm [0] = x / 60.0; // X indent
        _myTexpr.m_t_razm [1] = y / 60.0; // Y indent
        _myTexpr.m_t_razm [2] = x- (x / 60.0) * 2.0; // width (window) x
        _myTexpr.m_t_razm [3] = y / 4.0; // height (window) y
                qDebug () << "x:" << x << "_myTexpr.m_t_razm [0]:" << _myTexpr.m_t_razm [0];
                qDebug () << "x:" << x << "_myTexpr.m_t_razm [1]:" << _myTexpr.m_t_razm [1];
                qDebug () << "y:" << y << "_myTexpr.m_t_razm [2]:" << _myTexpr.m_t_razm [2];
                qDebug () << "y:" << y << "_myTexpr.m_t_razm [3]:" << _myTexpr.m_t_razm [3];
    }
    

    here is the announcement

      oknonastr = new Widgets :: oknonastroek (this);
    

    here is an update

         oknonastr-> update ();
    

    if there was ui, then the file attached to the project, then the turquoise square would be beautifully displayed and so straight it is sad

    введите сюда описание изображения

    Pl45m4P 1 Reply Last reply
    0
    • timob256T timob256

      @mpergand

      OknoWibora::OknoWibora(QWidget *parent)
      : QMainWindow(parent)
      {
      // QGridLayout *mainLayout = new QGridLayout;
      // oknonastr = new Widgets::OknoNastroek(this);
      // mainLayout->addWidget(oknonastr);
      // mainLayout->addWidget(display);

      QGridLayout *mainLayout = new QGridLayout;
      oknonastr = new Widgets::OknoNastroek(this);
      mainLayout->addWidget(oknonastr);
      

      }

      the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(

      Screenshot_20211008_163939.png

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #9

      @timob256

      OK, I explain in details:

      When you don't use an UI from the Designer, you need to:

      1. set the central widget
      2. add a layout to the central widget
      3. add others widgets to the layout
      QWidget* centralWidget= new QWidget;
      setCentralWidget(centralWidget);
      QBoxLayout* layout= new QVBoxLayout;
      centralWidget->setLayout(layout);
      layout->addWidget( *yourTurquoiseWidget*);
      
      1 Reply Last reply
      3
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #2

        I think you need to put your 'turquoise widget' in a layout.

        1 Reply Last reply
        0
        • timob256T Offline
          timob256T Offline
          timob256
          wrote on last edited by
          #3

          @mpergand said in how to attach class to qt project without ui file?:

          turquoise widget

          OknoWibora::OknoWibora(QWidget *parent)
          : QMainWindow(parent)
          {
          display = new QLineEdit();

          QGridLayout *mainLayout = new QGridLayout;
          
          oknonastr = new Widgets::OknoNastroek(this);
          
          mainLayout->addWidget(oknonastr);
          mainLayout->addWidget(display);
          

          }

          M 1 Reply Last reply
          0
          • timob256T timob256

            created a class which added to the project without ui.
            I can't call it correctly.

            ** the class I'm embedding: **

            #ifndef OKNONASTROEK_H
            #define OKNONASTROEK_H
            
            #include <QMainWindow>
            #include <QDebug>
            #include <QPainter>
            #include <QPen>
            #include <QFont>
            #include <QFontMetrics>
            #include <QKeyEvent>
            #include <QRegion>
            #include <QPointF>
            #include <QGraphicsView>
            
            namespace Widgets {
            struct texnStructur
            {
                texnStructur () // immediately fill it with zeros
                {
                    for (uint i = 0; i <sizeof (m_t_razm) / sizeof (m_t_razm [0]); i ++)
                        m_t_razm [i] = 0;
                }
                float m_t_razm [12];
            };
            
            
            class window pane: public QWidget
            {
                Q_OBJECT
            
            public:
                windowspar (QWidget * parent = 0);
                ~ windowspawn ();
            
                void razmer_element (int x, int y);
            
            
            protected:
                void paintEvent (QPaintEvent * event);
            
            
                texnStructur _myTexpr;
            
            };
            }
            #endif // OKNONASTROEK_H
            

            ** here is the "body" of the class **

            #include "oknonastroek.h"
            
            Widgets :: windowastroek :: windowastroek (QWidget * parent)
                : QWidget (parent)
            {
            }
            
            Widgets :: windowspawn :: ~ windowpattern ()
            {
            
            }
            
            void Widgets :: oknonastroek :: razmer_element (int x, int y)
            {
                // set the size of the lines here
                _myTexpr.m_t_razm [0] = x / 60.0; // X indent
                _myTexpr.m_t_razm [1] = g / 4.0 + g / 60.0; // Y indent
                _myTexpr.m_t_razm [2] = x / 3.0; // width (window) x
                _myTexpr.m_t_razm [3] = g / 1.4; // height (window) y
            }
            
            void Widgets :: windowastroek :: paintEvent (QPaintEvent * event)
            {
                QPainter painter (this); // Create a renderer object
                QBrush bgfon (QColor (180,220,220), Qt :: SolidPattern); // set the background color and fill type (with background without background and fill type)
                painter.setPen (Qt :: NoPen); // paint brush if not needed
            
                painter.setRenderHint (QPainter :: Antialiasing); // remove sharp cubes
                painter.setBrush (bgfon); // set the background color
                razmer_element (this-> width (), this-> height ()); // set the size of the element
            
                painter.drawRect (_myTexpr.m_t_razm [0], _myTexpr.m_t_razm [1], _myTexpr.m_t_razm [2], _myTexpr.m_t_razm [3]);
            }
            

            ** And here's the main class: **

            #ifndef OKNOWIBORA_H
            #define OKNOWIBORA_H
            
            #include <QMainWindow>
            #include <QDebug>
            #include <QPainter>
            #include <QPen>
            #include <QFont>
            #include <QFontMetrics>
            #include <QKeyEvent>
            #include <QRegion>
            #include <QPointF>
            #include <QGraphicsView>
            
            #include "oknonastroek.h"
            
            
            
            struct texnStructur
            {
                texnStructur () // immediately fill it with zeros
                {
                    for (uint i = 0; i <sizeof (m_t_razm) / sizeof (m_t_razm [0]); i ++)
                        m_t_razm [i] = 0;
                }
                
                float m_t_razm [12];
            };
            
            WindowVibora class: public QMainWindow
            {
                Q_OBJECT
            
            public:
                WindowWibora (QWidget * parent = 0);
                ~ WindowVibora ();
            
                void razmer_element (int x, int y);
            
            
            protected:
                void paintEvent (QPaintEvent * event);
                Widgets :: windowstrip * oknonastr;
            
                texnStructur _myTexpr;
            };
            
            #endif // OKNOWIBORA_H
            

            ** "body" of the main class **

            #include "oknowibora.h"
            
            OknoWibora :: OknoWibora (QWidget * parent)
                : QMainWindow (parent)
            {
                oknonastr = new Widgets :: oknonastroek (this);
            }
            
            OknoWibora :: ~ OknoWibora ()
            {
            
            }
            
            void OknoWibora :: paintEvent (QPaintEvent * event)
            {
                QPainter painter (this); // Create a renderer object
                QBrush bgfon (QColor (220,180,220), Qt :: SolidPattern); // set the background color and fill type (with background without background and fill type)
            
                QPen pen_abris (Qt :: black, 2, Qt :: SolidLine, Qt :: FlatCap); // outline (compass) brush
                QPen pen_line (Qt :: black, 2, Qt :: SolidLine, Qt :: FlatCap); // brush for lines and text
            
                painter.setRenderHint (QPainter :: Antialiasing); // remove sharp cubes
                painter.setBrush (bgfon); // set the background color
            
                painter.setPen (Qt :: NoPen); // paint brush if not needed
                razmer_element (this-> width (), this-> height ()); // set the dimensions
                painter.drawRect (_myTexpr.m_t_razm [0], _myTexpr.m_t_razm [1], _myTexpr.m_t_razm [2], _myTexpr.m_t_razm [3]);
            
                oknonastr-> update ();
            
                qDebug () << "rere1";
            }
            void OknoWibora :: razmer_element (int x, int y)
            {
                // set the size of the lines here
                _myTexpr.m_t_razm [0] = x / 60.0; // X indent
                _myTexpr.m_t_razm [1] = y / 60.0; // Y indent
                _myTexpr.m_t_razm [2] = x- (x / 60.0) * 2.0; // width (window) x
                _myTexpr.m_t_razm [3] = y / 4.0; // height (window) y
                        qDebug () << "x:" << x << "_myTexpr.m_t_razm [0]:" << _myTexpr.m_t_razm [0];
                        qDebug () << "x:" << x << "_myTexpr.m_t_razm [1]:" << _myTexpr.m_t_razm [1];
                        qDebug () << "y:" << y << "_myTexpr.m_t_razm [2]:" << _myTexpr.m_t_razm [2];
                        qDebug () << "y:" << y << "_myTexpr.m_t_razm [3]:" << _myTexpr.m_t_razm [3];
            }
            

            here is the announcement

              oknonastr = new Widgets :: oknonastroek (this);
            

            here is an update

                 oknonastr-> update ();
            

            if there was ui, then the file attached to the project, then the turquoise square would be beautifully displayed and so straight it is sad

            введите сюда описание изображения

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #4

            @timob256 said in how to attach class to qt project without ui file?:

            windowspar (QWidget * parent = 0);
            ~ windowspawn ();

            Typo or does your class c'tor have a different name than your d'tor?


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            timob256T 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @timob256 said in how to attach class to qt project without ui file?:

              windowspar (QWidget * parent = 0);
              ~ windowspawn ();

              Typo or does your class c'tor have a different name than your d'tor?

              timob256T Offline
              timob256T Offline
              timob256
              wrote on last edited by
              #5

              @Pl45m4 ????? I don't have such variables c'tor and d'tor

              artwawA 1 Reply Last reply
              0
              • timob256T timob256

                @Pl45m4 ????? I don't have such variables c'tor and d'tor

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by artwaw
                #6

                @timob256 c'tor - constructor, d'tor - destructor.
                Not vars but methods and yes, you have them.

                For more information please re-read.

                Kind Regards,
                Artur

                1 Reply Last reply
                0
                • timob256T timob256

                  @mpergand said in how to attach class to qt project without ui file?:

                  turquoise widget

                  OknoWibora::OknoWibora(QWidget *parent)
                  : QMainWindow(parent)
                  {
                  display = new QLineEdit();

                  QGridLayout *mainLayout = new QGridLayout;
                  
                  oknonastr = new Widgets::OknoNastroek(this);
                  
                  mainLayout->addWidget(oknonastr);
                  mainLayout->addWidget(display);
                  

                  }

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #7

                  @timob256 said in how to attach class to qt project without ui file?:

                  QGridLayout *mainLayout = new QGridLayout;

                  oknonastr = new Widgets::OknoNastroek(this);

                  mainLayout->addWidget(oknonastr);
                  mainLayout->addWidget(display);

                  You're missing something ;)
                  Find a perfect example for you:
                  https://github.com/HBPVIS/Servus/blob/master/apps/servusBrowser.cpp

                  timob256T 1 Reply Last reply
                  1
                  • M mpergand

                    @timob256 said in how to attach class to qt project without ui file?:

                    QGridLayout *mainLayout = new QGridLayout;

                    oknonastr = new Widgets::OknoNastroek(this);

                    mainLayout->addWidget(oknonastr);
                    mainLayout->addWidget(display);

                    You're missing something ;)
                    Find a perfect example for you:
                    https://github.com/HBPVIS/Servus/blob/master/apps/servusBrowser.cpp

                    timob256T Offline
                    timob256T Offline
                    timob256
                    wrote on last edited by
                    #8

                    @mpergand

                    OknoWibora::OknoWibora(QWidget *parent)
                    : QMainWindow(parent)
                    {
                    // QGridLayout *mainLayout = new QGridLayout;
                    // oknonastr = new Widgets::OknoNastroek(this);
                    // mainLayout->addWidget(oknonastr);
                    // mainLayout->addWidget(display);

                    QGridLayout *mainLayout = new QGridLayout;
                    oknonastr = new Widgets::OknoNastroek(this);
                    mainLayout->addWidget(oknonastr);
                    

                    }

                    the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(

                    Screenshot_20211008_163939.png

                    M JonBJ timob256T 3 Replies Last reply
                    0
                    • timob256T timob256

                      @mpergand

                      OknoWibora::OknoWibora(QWidget *parent)
                      : QMainWindow(parent)
                      {
                      // QGridLayout *mainLayout = new QGridLayout;
                      // oknonastr = new Widgets::OknoNastroek(this);
                      // mainLayout->addWidget(oknonastr);
                      // mainLayout->addWidget(display);

                      QGridLayout *mainLayout = new QGridLayout;
                      oknonastr = new Widgets::OknoNastroek(this);
                      mainLayout->addWidget(oknonastr);
                      

                      }

                      the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(

                      Screenshot_20211008_163939.png

                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by
                      #9

                      @timob256

                      OK, I explain in details:

                      When you don't use an UI from the Designer, you need to:

                      1. set the central widget
                      2. add a layout to the central widget
                      3. add others widgets to the layout
                      QWidget* centralWidget= new QWidget;
                      setCentralWidget(centralWidget);
                      QBoxLayout* layout= new QVBoxLayout;
                      centralWidget->setLayout(layout);
                      layout->addWidget( *yourTurquoiseWidget*);
                      
                      1 Reply Last reply
                      3
                      • timob256T timob256

                        @mpergand

                        OknoWibora::OknoWibora(QWidget *parent)
                        : QMainWindow(parent)
                        {
                        // QGridLayout *mainLayout = new QGridLayout;
                        // oknonastr = new Widgets::OknoNastroek(this);
                        // mainLayout->addWidget(oknonastr);
                        // mainLayout->addWidget(display);

                        QGridLayout *mainLayout = new QGridLayout;
                        oknonastr = new Widgets::OknoNastroek(this);
                        mainLayout->addWidget(oknonastr);
                        

                        }

                        the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(

                        Screenshot_20211008_163939.png

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @timob256
                        @mpergand has explained. Just to add, setCentralWidget() is a QMainWindow thing only.

                        1 Reply Last reply
                        0
                        • timob256T timob256

                          @mpergand

                          OknoWibora::OknoWibora(QWidget *parent)
                          : QMainWindow(parent)
                          {
                          // QGridLayout *mainLayout = new QGridLayout;
                          // oknonastr = new Widgets::OknoNastroek(this);
                          // mainLayout->addWidget(oknonastr);
                          // mainLayout->addWidget(display);

                          QGridLayout *mainLayout = new QGridLayout;
                          oknonastr = new Widgets::OknoNastroek(this);
                          mainLayout->addWidget(oknonastr);
                          

                          }

                          the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(

                          Screenshot_20211008_163939.png

                          timob256T Offline
                          timob256T Offline
                          timob256
                          wrote on last edited by
                          #11

                          @timob256 said in how to attach class to qt project without ui file?:

                          @mpergand

                          OknoWibora::OknoWibora(QWidget *parent)
                          : QMainWindow(parent)
                          {
                          // QGridLayout *mainLayout = new QGridLayout;
                          // oknonastr = new Widgets::OknoNastroek(this);
                          // mainLayout->addWidget(oknonastr);
                          // mainLayout->addWidget(display);

                          QGridLayout *mainLayout = new QGridLayout;
                          oknonastr = new Widgets::OknoNastroek(this);
                          mainLayout->addWidget(oknonastr);
                          

                          }

                          the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(

                          Screenshot_20211008_163939.png

                          this work
                          thsanks mpergand

                          jsulmJ 1 Reply Last reply
                          0
                          • timob256T timob256

                            @timob256 said in how to attach class to qt project without ui file?:

                            @mpergand

                            OknoWibora::OknoWibora(QWidget *parent)
                            : QMainWindow(parent)
                            {
                            // QGridLayout *mainLayout = new QGridLayout;
                            // oknonastr = new Widgets::OknoNastroek(this);
                            // mainLayout->addWidget(oknonastr);
                            // mainLayout->addWidget(display);

                            QGridLayout *mainLayout = new QGridLayout;
                            oknonastr = new Widgets::OknoNastroek(this);
                            mainLayout->addWidget(oknonastr);
                            

                            }

                            the code works but not as I need it, the turquoise square does not stretch, in a separate project it is perfectly stretched to the specified values :(

                            Screenshot_20211008_163939.png

                            this work
                            thsanks mpergand

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @timob256 said in how to attach class to qt project without ui file?:

                            mainLayout

                            You did not add this layout to any widget or other layout. Don't know how this can work...

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            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