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. Custom widget not showing
Forum Updated to NodeBB v4.3 + New Features

Custom widget not showing

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.3k 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.
  • E Offline
    E Offline
    eyrkon
    wrote on last edited by
    #1

    Hi,

    I am new to Qt and I am trying to create some custom widgets for my application.Here is my issue:

    I have the following main :

        QApplication a(argc, argv);
        QtWidgetsApplication w;
    
        QWidget* central_widget = new QWidget(&w);
    
        QListWidget* listWidget = new QListWidget(central_widget);
        listWidget->setObjectName(QString::fromUtf8("listWidget"));
    listWidget->setGeometry(QRect(20, 20, 256, 192));
    
        w.setCentralWidget(central_widget);
    
        w.show();
        return a.exec();
    

    This works fine. However if I create a basic custom widget of my own that inherits from QListWidget like this :

    class DeviceList : public QListWidget {
    
    public:
    	DeviceList(QObject* parent) {}
    };
    

    and replace

    QListWidget* listWidget = new QListWidget(central_widget);
    

    with

    DeviceList* listWidget = new DeviceList(central_widget);
    

    then my widget will not show and the main window is empty.
    If however I make the custom widget itself the central one

        w.setCentralWidget(listWidget);
    

    then it will actually appear.

    I am not sure what I am doing wrong but I feel like I am missing something fundamental here when it comes to custom widget inheritance.

    jsulmJ 1 Reply Last reply
    0
    • E eyrkon

      Hi,

      I am new to Qt and I am trying to create some custom widgets for my application.Here is my issue:

      I have the following main :

          QApplication a(argc, argv);
          QtWidgetsApplication w;
      
          QWidget* central_widget = new QWidget(&w);
      
          QListWidget* listWidget = new QListWidget(central_widget);
          listWidget->setObjectName(QString::fromUtf8("listWidget"));
      listWidget->setGeometry(QRect(20, 20, 256, 192));
      
          w.setCentralWidget(central_widget);
      
          w.show();
          return a.exec();
      

      This works fine. However if I create a basic custom widget of my own that inherits from QListWidget like this :

      class DeviceList : public QListWidget {
      
      public:
      	DeviceList(QObject* parent) {}
      };
      

      and replace

      QListWidget* listWidget = new QListWidget(central_widget);
      

      with

      DeviceList* listWidget = new DeviceList(central_widget);
      

      then my widget will not show and the main window is empty.
      If however I make the custom widget itself the central one

          w.setCentralWidget(listWidget);
      

      then it will actually appear.

      I am not sure what I am doing wrong but I feel like I am missing something fundamental here when it comes to custom widget inheritance.

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

      @eyrkon said in Custom widget not showing:

      DeviceList(QObject* parent) {}

      You forgot to call QListWidget constructor here passing it the parent.

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

      E JonBJ 2 Replies Last reply
      2
      • jsulmJ jsulm

        @eyrkon said in Custom widget not showing:

        DeviceList(QObject* parent) {}

        You forgot to call QListWidget constructor here passing it the parent.

        E Offline
        E Offline
        eyrkon
        wrote on last edited by
        #3

        @jsulm Whoops ,thanks, that works :)

        1 Reply Last reply
        0
        • E eyrkon has marked this topic as solved on
        • jsulmJ jsulm

          @eyrkon said in Custom widget not showing:

          DeviceList(QObject* parent) {}

          You forgot to call QListWidget constructor here passing it the parent.

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

          @jsulm
          I assume C++ always calls the base constructor which takes no parameters if you do not specify?
          And if there is no base with no parameters then it's an error?

          jsulmJ 1 Reply Last reply
          0
          • JonBJ JonB

            @jsulm
            I assume C++ always calls the base constructor which takes no parameters if you do not specify?
            And if there is no base with no parameters then it's an error?

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

            @JonB The base constructor here does not set the parent

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

            JonBJ 1 Reply Last reply
            2
            • jsulmJ jsulm

              @JonB The base constructor here does not set the parent

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

              @jsulm
              I know, that was not the question! :) The question (well, two questions) is about C++ behaviour as I phrased it, please? :) Don't make me go ask ChatGPT, she & I have had some disagreements about facts... !

              Pl45m4P 1 Reply Last reply
              0
              • JonBJ JonB

                @jsulm
                I know, that was not the question! :) The question (well, two questions) is about C++ behaviour as I phrased it, please? :) Don't make me go ask ChatGPT, she & I have had some disagreements about facts... !

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

                @JonB said in Custom widget not showing:

                The question (well, two questions) is about C++ behaviour as I phrased it, please?

                I'm not @jsulm , but you are right. Since most QWidget-based classes have nullptr as default arg for parent in their c'tor, it's basically empty. And a QObject / QWidget with no parent is "standalone" or a toplevel-widget and needs widget.show() to be visible.
                When there would be no default and no other empty base/super class c'tor available, I think it wouldn't even compile.

                Don't make me go ask ChatGPT, she & I have had some disagreements about facts... !

                WDYM?! Elephants do not lay eggs?!
                (Source)

                :D


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

                ~E. W. Dijkstra

                JonBJ 1 Reply Last reply
                2
                • Pl45m4P Pl45m4

                  @JonB said in Custom widget not showing:

                  The question (well, two questions) is about C++ behaviour as I phrased it, please?

                  I'm not @jsulm , but you are right. Since most QWidget-based classes have nullptr as default arg for parent in their c'tor, it's basically empty. And a QObject / QWidget with no parent is "standalone" or a toplevel-widget and needs widget.show() to be visible.
                  When there would be no default and no other empty base/super class c'tor available, I think it wouldn't even compile.

                  Don't make me go ask ChatGPT, she & I have had some disagreements about facts... !

                  WDYM?! Elephants do not lay eggs?!
                  (Source)

                  :D

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

                  @Pl45m4 Sadly ChatGPT no longer gives that answer to that question :(

                  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