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. Instancing multiple QLabels holding images and moving them independently
Forum Updated to NodeBB v4.3 + New Features

Instancing multiple QLabels holding images and moving them independently

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 288 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.
  • G Offline
    G Offline
    Gladiu
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • G Gladiu

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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
      0
      • jsulmJ jsulm

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

        G Offline
        G Offline
        Gladiu
        wrote on last edited by
        #3

        @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?

        artwawA jsulmJ 2 Replies Last reply
        0
        • G Gladiu

          @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?

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @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
          0
          • G Gladiu

            @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?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved