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. Clicking And Draging A QLabel That Is Created Dynamically (at runtime)
Qt 6.11 is out! See what's new in the release blog

Clicking And Draging A QLabel That Is Created Dynamically (at runtime)

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.8k 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.
  • E Offline
    E Offline
    EverydayDiesel
    wrote on last edited by
    #1

    I have an application which needs to create qlabels at runtime.

    To keep things simple we will just deal with one label all though the app will create as many as needed.

    How can I add code that will allow me to click and drag this label somewhere else on the screen?

    @
    void MainWindow::on_cmdCreateLabel_clicked()
    {
    QLabel* lbl;
    lbl = new QLabel("test ", ui->grpLabels);
    lbl->setGeometry(10, 20 + 20, 100, 18);
    lbl->setStyleSheet("QLabel { background-color : black; color : red; }");
    lbl->show();
    }
    @

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      What is the purpose of dragging the labels around?

      QLabels are designed to be stationary. Would the "Graphics View Framework":http://qt-project.org/doc/qt-5/qtwidgets-graphicsview-dragdroprobot-example.html or "Qt Quick":http://qt-project.org/doc/qt-5/qtquick-draganddrop-example.html suit your use case better?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • E Offline
        E Offline
        EverydayDiesel
        wrote on last edited by
        #3

        It doesnt have to be labels. I just need to be able to write text in them.

        The key that I have yet to figure out is how to add what i call (event handlers from windows programming) that will capture the drag drops when the objects are created dynamically at runtime.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          See the links I posted. They show you how to implement drag and drop in the Graphics View Framework and in Qt Quick.

          Also please describe what you want to do with the draggable labels, and how you want them to interact with the other components of your program. Your answer will influence the best way to implement this.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • E Offline
            E Offline
            EverydayDiesel
            wrote on last edited by
            #5

            I appreciate the input. I have been reading over the robot example for the past few days and to be honest, it is quite complex (at least for what I am trying to do)

            Basically I am trying to create an application that has a few predefined products and the user can drag them to see what they look like visually when put together.

            I was using labels because i found this code which allows you to load images
            @
            QPixmap pix("C:\your_image.jpg");
            ui->label_3->setPixmap(pix.scaled(500, 500,
            Qt::KeepAspectRatio, Qt::SmoothTransformation));@

            At the moment I am looking at the qt quick example but this seems very similar to XAML which is much different then the 'win forms' im use to (my background is c# but I am trying to learn QT/C++)

            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