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. Add AnalogClock widget into BoxLayout
Qt 6.11 is out! See what's new in the release blog

Add AnalogClock widget into BoxLayout

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 3.8k 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.
  • Y Offline
    Y Offline
    YDLU
    wrote on last edited by
    #1

    Hi
    I am new Qt developer, try to learn from examples.

    I try add "AnalogClock" widget, from http://cartan.cas.suffolk.edu/qtdocs/widgets-analogclock.html , to a QHBoxLayout or QVBoxLayout. Run the example was no problem. But the "Clock" widget can NOT show on the "BoxLayout".

    Why? or something I need do?

    Thanks
    YDLU

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Please show us the code you produced?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        What might be the problem here, is that the widget has no size hint and no minimum size hint.
        I would add that, it should fix the problem.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          YDLU
          wrote on last edited by
          #4

          the main.cpp code:
          I try to added a "Current time on top window and clock on rest of window. And try to learn how to custom a widget.

          Thanks
          Lu

          @
          ////////////////////////////////////////////////////

          #include <QApplication>
          #include <QtGui>

          #include "analogclock.h"

          int main(int argc, char* argv[])
          {
          QApplication app(argc, argv);

          // main window
          QWidget* window = new QWidget;
          // main layout
          QVBoxLayout* mainLayout = new QVBoxLayout();
          
          // tow layouts in main layout
          QHBoxLayout* hLayout = new QHBoxLayout;
          QVBoxLayout* vLayout = new QVBoxLayout;
          
          // add current time on top of window
          qDebug() << QTime::currentTime().toString();
          QLabel* label = new QLabel("<H2>"+QTime::currentTime().toString()+"</H2>");
          label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
          
          // a test widget
          QDial* dial = new QDial();
          dial->setRange(0, 120);
          dial->setWrapping(true);
          dial->setValue(0);
          
          // add clock widget on V layout
          AnalogClock* clock = new AnalogClock();
          //clock->show();
          vLayout->addWidget(clock);
          
          hLayout->addWidget(label);
          vLayout->addWidget(dial);
          
          mainLayout->addLayout(hLayout);
          mainLayout->addLayout(vLayout);
          
          window->setLayout(mainLayout);
          
          window->show();
          
          return app.exec&#40;&#41;;
          

          }
          @

          Edit: added @ tags around your code. Please do that yourself next time, so the code becomes more readable; Andre

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            You can see the analog clock once you enlarge the window manually.

            The cause for the "invisible" clock is that it does not provide a size hint. Add this to your clock class and it works as expected:

            @
            // in the header file:
            public:
            QSize sizeHint() const;

            // in the implementation:
            QSize AnalogClock::sizeHint() const
            {
            return QSize(100, 100);
            }
            @

            Just another hint, your hLayout and vLayout is not neede, it is sufficient to put all your widgets into the main layout like this:

            @
            mainLayout->addWidget(label);
            mainLayout->addWidget(clock);
            mainLayout->addWidget(dial);
            @

            http://www.catb.org/~esr/faqs/smart-questions.html

            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