Add AnalogClock widget into BoxLayout



  • 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



  • Please show us the code you produced?



  • 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.



  • 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



  • 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);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.