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. Working with different buttons type

Working with different buttons type

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 408 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.
  • N Offline
    N Offline
    n34rt
    wrote on last edited by
    #1
    template <typename Control>
    void AddQLabel(QLabelStruct* qls, Control* control)
    { 
        QLabel* label = new QLabel(qls->text, control); // <-- this works
        //....
    }
    
    
    
    void AddQLabel(QLabelStruct* qls, QObject* control)
    {
        QLabel* label = new QLabel(qls->text, control); // <-- fails because of control type
       //...
    }
    
    
    
    Main::Main(QWidget* parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
    
        QObject* btn = qobject_cast<QToolButton*>(ui.toolButton);
        AddQLabel(&qls, btn); // <-- fail cant use QOBject on new QLabel
    
        AddQLabel(&qls, ui.toolButton); // <-- works
    }
    

    Is it possible to write such a function that could work with button, toolbutton, checkbox, etc
    without using a template?

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

      Because widgets need a QWidget* as parent in their constructors.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        n34rt
        wrote on last edited by
        #2

        Got it working using:

        void AddQLabel(QLabelStruct* qls, QAbstractButton* control)
        {
        }
        
        1 Reply Last reply
        0
        • N n34rt
          template <typename Control>
          void AddQLabel(QLabelStruct* qls, Control* control)
          { 
              QLabel* label = new QLabel(qls->text, control); // <-- this works
              //....
          }
          
          
          
          void AddQLabel(QLabelStruct* qls, QObject* control)
          {
              QLabel* label = new QLabel(qls->text, control); // <-- fails because of control type
             //...
          }
          
          
          
          Main::Main(QWidget* parent)
              : QMainWindow(parent)
          {
              ui.setupUi(this);
          
              QObject* btn = qobject_cast<QToolButton*>(ui.toolButton);
              AddQLabel(&qls, btn); // <-- fail cant use QOBject on new QLabel
          
              AddQLabel(&qls, ui.toolButton); // <-- works
          }
          

          Is it possible to write such a function that could work with button, toolbutton, checkbox, etc
          without using a template?

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

          @n34rt said in Working with different buttons type:

          void AddQLabel(QLabelStruct* qls, QObject* control)

          use QWidget instead.

          N 1 Reply Last reply
          0
          • M mpergand

            @n34rt said in Working with different buttons type:

            void AddQLabel(QLabelStruct* qls, QObject* control)

            use QWidget instead.

            N Offline
            N Offline
            n34rt
            wrote on last edited by
            #4

            @mpergand thank you, whats the difference?

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

              Because widgets need a QWidget* as parent in their constructors.

              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