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. [SOLVED] Extending widgets

[SOLVED] Extending widgets

Scheduled Pinned Locked Moved General and Desktop
widgetcustom
14 Posts 3 Posters 8.4k 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.
  • N Offline
    N Offline
    nitzan
    wrote on 21 Sept 2015, 11:40 last edited by nitzan
    #1

    I'm trying to figure out what's the best way to extend a widget.

    I want to have a QLabel with a hover behavior, and as it seems from my research so far, the only way to do that is to extend the QLabel widget and and override the HoverEnter, HoverMove and HoverLeave methods.

    My question though is how do I then use my extended widget instead of the QLabel in the design view of Qt Creator?
    Do I actually need to go through all of this to do that?
    Is it enough to promote my widget?
    This isn't very clear how to do something that seems to be very basic..

    Thanks.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 21 Sept 2015, 11:55 last edited by
      #2

      Hi

      The promote feature is an easy way to use a custom widget in Designer.

      The full plugin road is for widgets where you want to be able to alter
      properties design time.

      N 1 Reply Last reply 21 Sept 2015, 11:58
      1
      • M mrjj
        21 Sept 2015, 11:55

        Hi

        The promote feature is an easy way to use a custom widget in Designer.

        The full plugin road is for widgets where you want to be able to alter
        properties design time.

        N Offline
        N Offline
        nitzan
        wrote on 21 Sept 2015, 11:58 last edited by
        #3

        Ok, then promoting sounds like the option for me.
        What do I need to provide for that? cpp and h files?

        M 1 Reply Last reply 21 Sept 2015, 12:00
        0
        • N nitzan
          21 Sept 2015, 11:58

          Ok, then promoting sounds like the option for me.
          What do I need to provide for that? cpp and h files?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 21 Sept 2015, 12:00 last edited by
          #4

          @nitzan
          Yes, just a normal cpp and h file.
          Just make sure you type ClassName (of your widget) correctly as it don't validate it :)

          N 1 Reply Last reply 21 Sept 2015, 13:16
          1
          • M mrjj
            21 Sept 2015, 12:00

            @nitzan
            Yes, just a normal cpp and h file.
            Just make sure you type ClassName (of your widget) correctly as it don't validate it :)

            N Offline
            N Offline
            nitzan
            wrote on 21 Sept 2015, 13:16 last edited by
            #5

            @mrjj

            thanks!
            that worked well, except for one problem.
            calling the setTest method seems to have no effect at all..
            I'm doing this:

            MyWidget::MyWidget(QWidget* parent) : QLabel(parent) {
            	qDebug() << "MyWidget ctor";
            	this->setText("hey there");
            	this->setAttribute(Qt::WA_Hover);
            }
            
            bool MyWidget::event(QEvent* event) {
            	switch (event->type()) {
            		case QEvent::HoverEnter:
            			this->setStyleSheet("color: rgb(255, 0, 0)");
            			break;
            
            		case QEvent::HoverLeave:
            			this->setStyleSheet("color: rgb(0, 0, 0)");
            			break;
            
            		default:
            			return QLabel::event(event);
            	}
            
            	return true;
            }
            

            And the label has no text in it.
            Any idea why?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 21 Sept 2015, 13:25 last edited by
              #6

              @nitzan said:

              this->setText(..)

              Have you tried SetText from outside, like via a button ?
              Sometimes I get fooled by stuff overwritten by the ui->setup which is run after
              your constructor.

              N 1 Reply Last reply 21 Sept 2015, 13:27
              0
              • M mrjj
                21 Sept 2015, 13:25

                @nitzan said:

                this->setText(..)

                Have you tried SetText from outside, like via a button ?
                Sometimes I get fooled by stuff overwritten by the ui->setup which is run after
                your constructor.

                N Offline
                N Offline
                nitzan
                wrote on 21 Sept 2015, 13:27 last edited by
                #7

                @mrjj
                but that creates coupling i would prefer to avoid..
                is there anyway to have that in the same class in a way that will be called after the ui->setup?

                M 1 Reply Last reply 21 Sept 2015, 13:32
                0
                • N nitzan
                  21 Sept 2015, 13:27

                  @mrjj
                  but that creates coupling i would prefer to avoid..
                  is there anyway to have that in the same class in a way that will be called after the ui->setup?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 21 Sept 2015, 13:32 last edited by
                  #8

                  @nitzan
                  It was just for testing.

                  Have you tried to set it on the label you promote. ?
                  And not call SetText at all in constructor.

                  I get the text here. (from designer)

                  N 1 Reply Last reply 21 Sept 2015, 13:40
                  0
                  • M mrjj
                    21 Sept 2015, 13:32

                    @nitzan
                    It was just for testing.

                    Have you tried to set it on the label you promote. ?
                    And not call SetText at all in constructor.

                    I get the text here. (from designer)

                    N Offline
                    N Offline
                    nitzan
                    wrote on 21 Sept 2015, 13:40 last edited by
                    #9

                    @mrjj
                    calling setText from outside the class works well, even if I do it from the event method it works, but not in the ctor.

                    I can't set the text from the designer as the actual text I'm trying to put there is from font awesome (using QtAwesome), and so the actual code is:

                    UserWidget::UserWidget(QWidget* parent) : QLabel(parent) {
                    	qDebug() << "UserWidget ctor";
                    	this->setText(QString(QChar(fa::user)).append(" login"));
                    	this->setAttribute(Qt::WA_Hover);
                    }
                    
                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 21 Sept 2015, 13:47 last edited by Chris Kawa
                      #10

                      If you don't want the text overwritten by the text from designer then instead of promoting QLabel to your class promote a plain QWidget. It won't have the Text property in the editor so the generated setupUi code won't call setText and the value set in your constructor will be preserved.

                      N 1 Reply Last reply 21 Sept 2015, 13:54
                      1
                      • C Chris Kawa
                        21 Sept 2015, 13:47

                        If you don't want the text overwritten by the text from designer then instead of promoting QLabel to your class promote a plain QWidget. It won't have the Text property in the editor so the generated setupUi code won't call setText and the value set in your constructor will be preserved.

                        N Offline
                        N Offline
                        nitzan
                        wrote on 21 Sept 2015, 13:54 last edited by
                        #11

                        @Chris-Kawa
                        Sounds like a workaround, but I would like to understand why it's needed.

                        As the ctor of QLabel is called before the code in my widget is executed, then the setupUi code should be executed before my code as well.
                        Unless I'm missing something?

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 21 Sept 2015, 14:02 last edited by
                          #12

                          Well its the setupui for the mainform/dialog.
                          It creates the Qlabel and set its text.
                          So calls setText after the constructor.

                          1 Reply Last reply
                          1
                          • C Offline
                            C Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on 21 Sept 2015, 14:02 last edited by Chris Kawa
                            #13

                            It's not a workaround. You should always promote from the "lowest" widget of which functionality you want to preserve. In this case you don't want to have the label's ability to edit text in the designer so you promote from a widget, not label.

                            As the ctor of QLabel is called before the code in my widget is executed, then the setupUi code should be executed before my code as well. Unless I'm missing something?

                            The code of the window widget's constructor usually looks something like this:

                            Foo::Foo(QWidget* parent) : QWidget(parent) {
                                ui->setupUi(this);
                            }
                            

                            which "expands" to something like this:

                            Foo::Foo(QWidget* parent) : QWidget(parent) {
                                ...
                                ui->someLabel = new YourLabel(this);
                                ui->someLabel->setText(whateverWasSetinTheDesigner);
                                ...
                            }
                            

                            so as you can see whatever you set in the constructor is immediately overwritten by what was set in the designer.
                            If you promote from a QWidget the designer doesn't "know" it's a label and it won't generate the setText() call.

                            N 1 Reply Last reply 21 Sept 2015, 14:12
                            1
                            • C Chris Kawa
                              21 Sept 2015, 14:02

                              It's not a workaround. You should always promote from the "lowest" widget of which functionality you want to preserve. In this case you don't want to have the label's ability to edit text in the designer so you promote from a widget, not label.

                              As the ctor of QLabel is called before the code in my widget is executed, then the setupUi code should be executed before my code as well. Unless I'm missing something?

                              The code of the window widget's constructor usually looks something like this:

                              Foo::Foo(QWidget* parent) : QWidget(parent) {
                                  ui->setupUi(this);
                              }
                              

                              which "expands" to something like this:

                              Foo::Foo(QWidget* parent) : QWidget(parent) {
                                  ...
                                  ui->someLabel = new YourLabel(this);
                                  ui->someLabel->setText(whateverWasSetinTheDesigner);
                                  ...
                              }
                              

                              so as you can see whatever you set in the constructor is immediately overwritten by what was set in the designer.
                              If you promote from a QWidget the designer doesn't "know" it's a label and it won't generate the setText() call.

                              N Offline
                              N Offline
                              nitzan
                              wrote on 21 Sept 2015, 14:12 last edited by
                              #14

                              Alright, promoting from QWidget indeed did the job and now it works well!
                              Thanks for the both of you.

                              1 Reply Last reply
                              0

                              9/14

                              21 Sept 2015, 13:40

                              • Login

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