Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Instancing multiple QLabels holding images and moving them independently

    General and Desktop
    3
    5
    96
    Loading More Posts
    • 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.
    • G
      Gladiu last edited by

      Hi, i posted this question on wrong category before. I hope its a good one I am using QTWidgets application.
      I need to be able to instance multiple Qlabels that hold images. I want user to be able to move those images freely. After extensive search on google, ive found that to do that i need code that looks something like this

      void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
      {
      QGridLayout gridLayout = findChild<QGridLayout>("gridLayout");
      Q_ASSERT(gridLayout);
      QLabel *label = new QLabel("Placeholder");
      gridLayout->addWidget(label);
      }

      And then to be able to move this label around i would need to move entire layout around?

      Is there a way to not create multiple layouts that would only hold one single label?
      Is there a class that would allow me to place and then move images freely in my window? By freely I mean moving it with pixel perfect precision.
      Would be cool if user could drag images around in designated area. But so far i just want to know if for each independent image i need to create a layout.

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Gladiu last edited by

        @Gladiu If you want to move the labels freely then do not put them into layouts.

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

        G 1 Reply Last reply Reply Quote 0
        • G
          Gladiu @jsulm last edited by

          @jsulm Hey, thanks for response. If I should avoid putting them in layouts then how do i go about putting them on my window?
          if in MainWindow class i do

          Qlabel label = new Qlabel("My cool label");
          this->addWidget(label);
          

          it tells me in terminal that i should use public API and my Qlabel isnt displayed at all. How do i go about displaying images without layout?

          artwaw jsulm 2 Replies Last reply Reply Quote 0
          • artwaw
            artwaw @Gladiu last edited by

            @Gladiu try ui->centralwidget->addWidget() instead.
            Your UI is just a member variable of MainWindow so you don't add the label to UI.

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply Reply Quote 0
            • jsulm
              jsulm Lifetime Qt Champion @Gladiu last edited by

              @Gladiu said in Instancing multiple QLabels holding images and moving them independently:

              then how do i go about putting them on my window?

              You simply pass the parent widget (the one where the widget you're adding should be located) as parent:

              Qlabel label = new Qlabel("My cool label", this); // this is the parent here
              

              No need for addWidget.

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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post