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. Widgets are not being shown
Qt 6.11 is out! See what's new in the release blog

Widgets are not being shown

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 628 Views 2 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.
  • T Offline
    T Offline
    TrueNova
    wrote on last edited by TrueNova
    #1

    Hello. I am working on my university project on the Space topic. I decided to do a model of Solar system, but I encountered a lot of problems. I have class Drawer, I am setting it as MainWindow layout, the class should draw me orbits of planets(Star sublevel) with QPainter and the planets itself, the planets are animated and circles around their orbits with the help of Qtimer. Planet is a widget with ui, that consists of Qlabel for planet image and, later, there will be text info close to it. The problem now is that my planets don't want to show on the screen, howewer, their parent is Drawer, and it is shown(orbits) correctly(one more question about it later).
    Here is my Drawer class

    class Drawer : public QWidget
    {
        Q_OBJECT
    public:
        explicit Drawer(Star *star, QWidget *parent = nullptr);
        ~Drawer();
        Star *star;
        vector<float> angles;
        vector<planetWidget*> widgets;
        QTimer *animation;
        void startClock(){
            animation=new QTimer(this);
            connect(animation, SIGNAL(timeout()), this, SLOT(handler()));
            animation->start(16);
        }
    private slots:
        void handler(){
            //something more
            drawPlanets();
        }
        void drawPlanets();
    protected:
        void paintEvent(QPaintEvent *event);
    };
    

    drawer.cpp

    Drawer::Drawer(Star *star, QWidget *parent) : QWidget(parent)
    {
        for(int i=0; i<10;i++){//fix later
            angles.push_back(rand()%360);
        }
        planetWidget *widget;
        this->star=star;
        Planet *planet;
        QPoint center(this->width()/2, this->height()/2);
        float x, y;
        for(int i=0, j=0; i<star->sublevelCount; i++, j=0){
            planet=star->sublevel[i];
            while(planet){
                QString path=":/images/images/planets/";
                path.append(QString::number((j+++1)%star->sublevelCount));
                path.append(".png");
                widget=new planetWidget(path, planet, this);
                widget->resize(250, 150);
                widgets.push_back(widget);
                planet=planet->next;
            }
        }
    }
    void Drawer::drawPlanets()
    {
        QPoint center(this->width()/2, this->height()/2);
        Planet *planet;
        float x, y;
        for(int i=0, j=0; i<star->sublevelCount; i++, j=0){
            planet=star->sublevel[i];
            while(planet){
                x=this->width()/2+(planet->orbit+20)*cos(angles[i])-100;
                y= this->height()/2+(planet->orbit+20)*sin(angles[i])-100;
                x=round(x);
                y=round(y);
                widgets[i]->move(x, y);
                planet=planet->next;
            }
            angles[i]+=0.001;
        }
    }
    

    But this is what I see on the screen, no planets here.
    59767a1c-d671-43ed-94c4-6034946d6f02-image.png
    I think it would be great if someone not gave me right code, but simply explained, what I am doing wrong. As I know, if you want to add widget to another widget and show it on the screen, you don't need to use layouts at all, but I could not find the solution on how to add Drawer on the screen without setting it as the main layout. The same thing with planets(but layouts did not work either). How should I add widget inside of a widget in general and what is my problem in particular here?
    Sorry for asking such stupid question, but I am pretty bad at English, so I was not able to find answers by myself.
    Thank you.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      In my experience , a widget not inside a layout is invisible par default.
      Adding setVisible(true) in the constructor solved the problem for me.

      T 1 Reply Last reply
      1
      • M mpergand

        In my experience , a widget not inside a layout is invisible par default.
        Adding setVisible(true) in the constructor solved the problem for me.

        T Offline
        T Offline
        TrueNova
        wrote on last edited by TrueNova
        #3

        @mpergand said in Widgets are not being shown:

        setVisible

        Adding setVisible to constructor in Drawer and also in Planet widget did something very strange.(Sun got right to the left upper corner)
        a43aeff4-9422-45ea-8025-dc33ed3befe2-image.png
        Also, planets started jumping back and forth on their orbits sometimes, also, if I press Alf+F4, the programm is not closing, but only one planet on the screen does.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TrueNova
          wrote on last edited by
          #4

          Yes, problem was here, but now strange this happened. Now my planets are in rectangles filled with (0;0) position's background, but I think I am able to fix it.
          I will be pleased if someone explained me if I did everything else related to this parenthness correct. And how to fix the thing with widgets, that if I click on the planet, and then press Alt+F4, only planet widget is being closed?

          Thank you for the solution, @mpergand!

          M 1 Reply Last reply
          0
          • T TrueNova

            Yes, problem was here, but now strange this happened. Now my planets are in rectangles filled with (0;0) position's background, but I think I am able to fix it.
            I will be pleased if someone explained me if I did everything else related to this parenthness correct. And how to fix the thing with widgets, that if I click on the planet, and then press Alt+F4, only planet widget is being closed?

            Thank you for the solution, @mpergand!

            M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            @TrueNova said in Widgets are not being shown:

            Now my planets are in rectangles filled with (0;0) position's background,

            Each widget is positionned relative to its parent's one

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Looking at your application and if you want to stay with widgets, you might want to consider using the graphics view framework .

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              T 1 Reply Last reply
              2
              • SGaistS SGaist

                Hi,

                Looking at your application and if you want to stay with widgets, you might want to consider using the graphics view framework .

                T Offline
                T Offline
                TrueNova
                wrote on last edited by
                #7

                @SGaist I am a newbie to Qt and I did not even know about this grafics view framework, thank you!

                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