Nominate our 2022 Qt Champions!

How to make a "global/shared" widget

  • Hello,

    First of all I'm quite new to C++ and Qt. I'm used to Java.
    I'm trying to make my first UI on Qt. I started with a StackedWidget because I want some kind of Home with buttons that open differents widgets.
    From here everything is ok.
    The problem is that I would like to have a common function on every widget. This widget would be at the top of the page and contains a label where I set date and time. At the moment I don't know how to achieve this.
    Here's what I've done for now :

    • MainWidget containing my StackedWidget
    • My StackedWidget contains 3 others widgets
    • I'm able to switch between my differents widget.

    What I'm not able to do is access the Labels I have in my others Widgets from my MainWidget in order to set the time as Text. I've already done the timer.

    I'm note sure that I've made myself clear.
    There is probably a problem in my app architecture, It's my first app and don't know what are the good practices.

    Thank for you help.

  • Lifetime Qt Champion

    @DavidM29 Add a setter method to your other widget to set the date/time:

    class MyWidget...
        void setDateTime(const QDateTime&);

  • @jsulm
    I used your idea of creating a setter in each other widget. It worked but it forces me to setDate on every single widget. But it does the trick.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Depending on what your other widgets are, you can create a base class that has that method and inherit your other widgets from it.

Log in to reply