What is Wrong with TestCase for Drag and Drop funcationality?
Unsolved
General and Desktop
-
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 atdrag->exec(Qt::CopyAction | Qt::MoveAction);
what is things which I am doing wrong in
testDropToBuilderContainer
?