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. Can QT create a widget and add it to the layout in one line ?
Forum Updated to NodeBB v4.3 + New Features

Can QT create a widget and add it to the layout in one line ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 210 Views 2 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.
  • J Offline
    J Offline
    John Van
    wrote on 1 Sept 2023, 11:45 last edited by
    #1

    In QT, this usually requires two lines of code, and I think the increase in the number of lines of code makes reading more complex. I used the template method to implement it on one line, but I still need to manually distinguish between controls and layouts. Is there a better implementation method?

    #include <QApplication>
    #include <QWidget>
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <QRadioButton>
    //#include "RangeSlider.h"
    
    template<class W, class L, class... Args>
    W*  NewW(L* l, Args&&... args)
    {
        auto w = new W(std::forward<Args>(args)...);
        l->addWidget(w);
        return w;
    }
    template<class L1, class L0, class... Args>
    L1* NewL(L0* l0, Args&&... args)
    {
        auto l1 = new L1(std::forward<Args>(args)...);
        l0->addLayout(l1);
        return l1;
    }
    class SelectZ : public QWidget
    {
    public:
        SelectZ()
        {
            auto h1 = new QHBoxLayout(this);
            //auto rs = NewW<RangeSlider>(h1, Qt::Vertical, RangeSlider::Option::DoubleHandles, this);
            auto v1 = NewL<QVBoxLayout>(h1, this);
    		auto h21 = NewL<QHBoxLayout>(h1, this);
    		QRadioButton* radioBtn1 = NewW<QRadioButton>(h21, u8"aaa", this);
    		QRadioButton* radioBtn2 = NewW< QRadioButton>(h21, u8"bbb", this);
    	}
    };
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
        SelectZ w;
        w.resize(800, 600);
        w.show();
        return a.exec();
    }
    
    
    P 1 Reply Last reply 1 Sept 2023, 12:08
    0
    • J John Van
      1 Sept 2023, 11:45

      In QT, this usually requires two lines of code, and I think the increase in the number of lines of code makes reading more complex. I used the template method to implement it on one line, but I still need to manually distinguish between controls and layouts. Is there a better implementation method?

      #include <QApplication>
      #include <QWidget>
      #include <QHBoxLayout>
      #include <QVBoxLayout>
      #include <QRadioButton>
      //#include "RangeSlider.h"
      
      template<class W, class L, class... Args>
      W*  NewW(L* l, Args&&... args)
      {
          auto w = new W(std::forward<Args>(args)...);
          l->addWidget(w);
          return w;
      }
      template<class L1, class L0, class... Args>
      L1* NewL(L0* l0, Args&&... args)
      {
          auto l1 = new L1(std::forward<Args>(args)...);
          l0->addLayout(l1);
          return l1;
      }
      class SelectZ : public QWidget
      {
      public:
          SelectZ()
          {
              auto h1 = new QHBoxLayout(this);
              //auto rs = NewW<RangeSlider>(h1, Qt::Vertical, RangeSlider::Option::DoubleHandles, this);
              auto v1 = NewL<QVBoxLayout>(h1, this);
      		auto h21 = NewL<QHBoxLayout>(h1, this);
      		QRadioButton* radioBtn1 = NewW<QRadioButton>(h21, u8"aaa", this);
      		QRadioButton* radioBtn2 = NewW< QRadioButton>(h21, u8"bbb", this);
      	}
      };
      
      int main(int argc, char* argv[])
      {
          QApplication a(argc, argv);
          SelectZ w;
          w.resize(800, 600);
          w.show();
          return a.exec();
      }
      
      
      P Offline
      P Offline
      Pl45m4
      wrote on 1 Sept 2023, 12:08 last edited by
      #2

      @John-Van

      How about

      layout->addWidget(new Widget());
      

      ?

      I dont think using two lines makes it any more complicated.
      And sometimes you dont want to add a widget directy to something


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

      ~E. W. Dijkstra

      1 Reply Last reply
      2

      1/2

      1 Sept 2023, 11:45

      • Login

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