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. What is Wrong with TestCase for Drag and Drop funcationality?

What is Wrong with TestCase for Drag and Drop funcationality?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtestmouse eventmouse controldrag and dropqt5.15.4
1 Posts 1 Posters 252 Views
  • 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.
  • Y Offline
    Y Offline
    Yash001
    wrote on last edited by Yash001
    #1

    I do have QLabel, which is has install event filter.

    QLabel* elementIconLabel = new QLabel(elementDispalyWidget);
    elementIconLabel->installEventFilter(new ElementDragEventHandler(elementIconLabel)); // Install event filter
    

    here my eventfilter class.

    bool ElementDragEventHandler::eventFilter(QObject* watched, QEvent* event)
    {
      if (event->type() == QEvent::MouseButtonPress) {
          QWidget* label = qobject_cast<QWidget*>(watched);
          if (label && label->property("element").isValid()) {
    
              // Get the element data stored in the label
              AbstractElement* element = qvariant_cast<AbstractElement*>(label->property("element"));
    
              // Create a drag object
              QDrag* drag = new QDrag(label);
              QMimeData* mimeData = new QMimeData;
    
              // Set the element as the mime data
              mimeData->setProperty("element", QVariant::fromValue(element));
    
              drag->setMimeData(mimeData);
    
              // Set the drag pixmap
              QPixmap pixmap = element->getImage().scaled(90, 90); // Adjust the size as needed
              drag->setPixmap(pixmap);
    
              // Start the drag
              drag->exec(Qt::CopyAction | Qt::MoveAction);
              return true;
          }
      }
    
      return QObject::eventFilter(watched, event);
    }
    

    Here My test case.

    void TestBuilderContainer::testDropToBuilderContainer()
    {
        MainAppWindow* mainWindow = MainWindowManager::getAppManage().getMainAppWindow();
        QVERIFY(mainWindow != nullptr);
    
        QWidget* tabDataWidget = mainWindow->findChild<QWidget*>("prebuiltElementsList");
        QTreeView* elementsListWidget = tabDataWidget->findChild<QTreeView*>("ElementsListWidget");
        QVERIFY(elementsListWidget != nullptr);
    
        BuilderContainer* builderContainer = mainWindow->findChild<BuilderContainer*>("BuilderContainer");
        QVERIFY(builderContainer != nullptr);
        
        // Get the model associated with the tree view
        QAbstractItemModel* model = elementsListWidget->model();
        QVERIFY(model != nullptr);
    
    
        for (int row = 0; row < model->rowCount(); ++row) {
            
            // Iterate through columns
            for (int column = 0; column < model->columnCount(); ++column) {
    
                QTest::qWait(100); // Wait for a moment to ensure the drag is initiated
                
       
                // Create an index for the current row and column
                QModelIndex currentIndex = model->index(row, column);
    
                // Check if the index is valid (it should be)
                QVERIFY(currentIndex.isValid());
    
                // Accessing a QLabel by object name within the current index
                QLabel* elementIconLabel = elementsListWidget->indexWidget(currentIndex)->findChild<QLabel*>("elementIconLabel");
    
                // Check if the QLabel with object name "elementNameLabel" exists
                if (elementIconLabel != nullptr) {
                   
                    // Calculate the center position of elementIconLabel
                    QPoint elementCenter = elementIconLabel->rect().center();
    
                    // Map the center position to global coordinates
                    QPoint globalElementCenter = elementIconLabel->mapToGlobal(elementCenter);
    
                    // Simulate a mouse press event at the center of elementIconLabel
                    QTest::mousePress(elementIconLabel, Qt::LeftButton, Qt::NoModifier, globalElementCenter);
                    QTest::qWait(100);
    
                    // Simulate the drag event
                    // QTest::mouseMove(elementsListWidget);
                    
                    // Calculate the center position of the builderContainer
                    QPoint builderContainerCenter = builderContainer->rect().center();
                    QTest::mouseMove(builderContainer, builderContainerCenter);
                    QTest::qWait(100);
    
                    QTest::mouseRelease(builderContainer, Qt::LeftButton, Qt::NoModifier, builderContainerCenter);
                    QTest::qWait(100);
    
                    
    
                }
            }
        }
    
    }
    

    here My Drop events

    void BuilderContainer::dropEvent(QDropEvent* event)
    {
        const QMimeData* mimeData = event->mimeData();
        const QVariant& elementPointer = mimeData->property("element");
        if (elementPointer.isValid()) {
            std::shared_ptr<AbstractElement> element = qvariant_cast<AbstractElement*>(elementPointer)->clone();
            if (element) {
    
                int insertIndex = findInsertIndex(event);
    
                addInformationAndView(element, insertIndex);
    
                hideDropIndicator(); // Hide the drop indicator
    
                emit updateResultedTextView(); // Update the text in ResultedTextView Widget.
                emit notifyToParameterWidgets(); // Update the Parameter widget in ParameterUIBuilder.
    
                event->acceptProposedAction();
            }
        }
    }
    

    above Test case is programatically working fine if I will move mouse manually in BuilderContainer frame. If I will not move mouse then execution will stuck at drag->exec(Qt::CopyAction | Qt::MoveAction);

    what is things which I am doing wrong in testDropToBuilderContainer ?

    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