PDA

View Full Version : Question about Drag and Drop



da447m
4th August 2018, 12:40
Hello, I'm a noob in Qt.

Currently, I have a hobby project of implementing a Chess program interface (that I hope I can, later on, load chess engines into but the idea is coding my own engine).

Anyway, I implemented things in the following manner:

1 - using mainwindow;
2 - creating a frame to be the chess board;
3 - creating 64 labels to be the pieces;

Now, I'm trying to use the Qt drag and drop examples to make it work. The first problem I have is that apparently I need to implement everything in the mainwindow class, can't seem to make separate classes work. Ideally, I would want that only the frame widget accepts drops, not the whole window (centralWidget). It only works if I set accept drops true for the main window, that is, in mainwindow.cpp I need:



MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
...

{


I've tried a work around for only my chessBoard frame object to accept drops by using




MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->chessBoard->setAcceptDrops(true);
ui->chessBoard->installEventFilter(this);
...

{

bool MainWindow::eventFilter(QObject *pFilterObj,QEvent *pEvent)
{
if ( (pFilterObj == ui->chessBoard) && (pEvent->type() == QEvent::DragEnter) )
{
QDragEnterEvent *dEvent = (QDragEnterEvent*)pEvent;
if (dEvent->mimeData()->hasFormat("application/x-dnditemdata")) dEvent->acceptProposedAction();
}
if ( (pFilterObj == ui->chessBoard) && (pEvent->type() == QEvent::Drop) )
{
QDropEvent *dEvent = (QDropEvent*)pEvent;
qDebug() << dEvent->mimeData()->data("application/x-dnditemdata");
}
return false;
}


But it doesn't work. Any variation I've tried still fails.

Second problem is that I can't seem to find a way to use of an ui form and have those objects available to change their properties in another class/source, because qmake(?) generates a separate class from the ui form objects and functions during compilation. I imagine I'd need to create actions and connect every single event I want for those objects, is that correct?

Let me put all of this in other terms: is it possible to (i) create a separate class/source for a widget that goes into the main window, in a fixed position, and that (ii) has properties not shared by the main window, such as accepting drops, etc?

As you can guess, the end result I want is to drag the squares of the chessboard around the chess board, drop them in one of the 64 positions exactly and set rules around the drop (chess moves rules, so cannot drop a white pawn on a square occupied by another white pawn, etc).