PDA

View Full Version : dragging cellwidgets in tablewidgets



hubipet
13th May 2009, 09:23
I have a mainwindow application with a tablewidget as centralwidget.

Looks like this:
class StripeArea_t : public QTableWidget
{
Q_OBJECT
public:

StripeArea_t(QWidget* _Parent = 0);


protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
void mousePressEvent(QMouseEvent *event);



};



The tablewidget has 1 column and various rows. As content of the tablecell I want to use a cellwidget something like a QFrame.

Looks like this:
class Stripe : public QFrame, public QTableWidgetItem
{
}



Now I try to drag and drop this cellwidget from one tablecell to another.
I have overwritten mousePressEvent(QMouseEvent *event) in the Stripe class
and the StripeArea class.
But no dragging takes place. What's the error???

talk2amulya
13th May 2009, 11:00
can you share some details of implementation..some code would be useful..

hubipet
13th May 2009, 14:54
Well the application consists 3 classes and I will share all I've done till now.

Mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QCloseEvent>
#include "ui_mainwindow.h"

class StripeArea_t;
class Stripe_t;

class MainWindow : public QMainWindow, public Ui_MainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:

StripeArea_t* LeftHandoverArea;
StripeArea_t* LeftStripeArea;
StripeArea_t* RightStripeArea;
StripeArea_t* RightHandoverArea;

Stripe_t* Stripe1;
Stripe_t* Stripe2;

bool CanClose;

void createStripeAreas();

void closeEvent(QCloseEvent*);
};

#endif // MAINWINDOW_H



Mainwindow.cpp


#include "Mainwindow.h"
#include "ui_mainwindow.h"
#include "StripeArea.h"
#include "Stripe.h"
#include <QApplication>
#include <QCleanlooksStyle>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QApplication::setStyle(new QCleanlooksStyle);
setupUi(this);
createStripeAreas();
// showFullScreen();


connect(actionClose, SIGNAL(triggered()), this, SLOT(close()));

LeftStripeArea->setColumnCount(1);
LeftStripeArea->setRowCount(1);
LeftStripeArea->setRowHeight(0,105);
Stripe1 = new Stripe_t(LeftStripeArea);
LeftStripeArea->setRowCount(2);
LeftStripeArea->setRowHeight(1,105);
LeftStripeArea->setCellWidget(0, 0, Stripe1);
Stripe2 = new Stripe_t(LeftStripeArea);
LeftStripeArea->setCellWidget(1, 0, Stripe2);
//

CanClose = true;
}

MainWindow::~MainWindow()
{
}


void MainWindow::createStripeAreas()
{

LeftHandoverArea = new StripeArea_t(LeftWidgetContents, StripeArea_t::HANDOVER);
LeftWidgetLayout->addWidget(LeftHandoverArea , 0, 0, 1, 1);


LeftStripeArea = new StripeArea_t(this, StripeArea_t::STRIPE);
MainLayout->addWidget(LeftStripeArea , 1, 1, 1, 1);



RightStripeArea = new StripeArea_t(this, StripeArea_t::STRIPE);
MainLayout->addWidget(RightStripeArea, 1, 2, 1, 1);



RightHandoverArea = new StripeArea_t(RightWidgetContents, StripeArea_t::HANDOVER);
RightWidgetLayout->addWidget(RightHandoverArea, 0, 1);
}







void MainWindow::closeEvent(QCloseEvent* CE)
{
if(CanClose)
{
CE->accept();
}
else
{
CE->ignore();
}
}





StripeArea.h


#ifndef STRIPEAREA_H
#define STRIPEAREA_H

#include <QTableWidget>
#include <QWidget>
#include <QtGui/QHeaderView>


class QMouseEvent;

class StripeArea_t : public QTableWidget
{
Q_OBJECT
public:
enum AreaType{NOAREA, STRIPE, HANDOVER};


StripeArea_t(QWidget* _Parent = 0, AreaType _Area = NOAREA);


protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
void mousePressEvent(QMouseEvent *event);
};

#endif // STRIPEAREA_H


StripeArea.cpp


#include <QMouseEvent>


#include "StripeArea.h"
#include "Stripe.h"
#include <iostream>



StripeArea_t::StripeArea_t(QWidget* _Parent, AreaType _Area)
: QTableWidget(_Parent)
{
QPalette palette;
QBrush brush;

switch(_Area)
{
case HANDOVER:
{
brush.setColor(QColor(255, 236, 188, 255));
break;
}
case STRIPE:
{
brush.setColor(QColor(171, 205, 255, 255));
break;
}
default:
{
break;
}
}

brush.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Base, brush);
palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
setPalette(palette);
setAutoFillBackground(true);
setFrameShape(QFrame::StyledPanel);
setFrameShadow(QFrame::Raised);
setAutoFillBackground(true);
horizontalHeader()->setVisible(false);
verticalHeader()->setVisible(false);
}


void StripeArea_t::mousePressEvent(QMouseEvent *event)
{
Stripe_t *child = static_cast<Stripe_t*>(childAt(event->pos()));
if (!child)
return;

QPoint hotSpot = event->pos() - child->pos();

QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << QPoint(hotSpot);

QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-EFS", itemData);

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setHotSpot(hotSpot);

child->hide();

if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
child->close();
else
child->show();

}

void StripeArea_t::dragEnterEvent(QDragEnterEvent *event)
{
}

void StripeArea_t::dragMoveEvent(QDragMoveEvent *event)
{
}

void StripeArea_t::dropEvent(QDropEvent *event)
{
}



Stripe.h

#ifndef STRIPE_H
#define STRIPE_H

#include <QFrame>

class Stripe_t : public QFrame
{
Q_OBJECT
public:
Stripe_t(QWidget* _parent = 0);

protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
void mousePressEvent(QMouseEvent *event);

};

#endif // STRIPE_H



Stripe.cpp


#include <QMouseEvent>

#include "Stripe.h"
#include <iostream>


Stripe_t::Stripe_t(QWidget* _Parent)
: QFrame(_Parent)
{
QPalette palette;
QBrush brush;
QBrush brush1;
brush.setColor(QColor(238, 238, 238, 255));
brush.setColor(QColor(255, 255, 255, 255));
brush.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Base, brush);
brush1.setColor(QColor(171, 205, 255, 255));
brush1.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Window, brush1);
palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
palette.setBrush(QPalette::Inactive, QPalette::Window, brush1);
palette.setBrush(QPalette::Disabled, QPalette::Base, brush1);
palette.setBrush(QPalette::Disabled, QPalette::Window, brush1);
setPalette(palette);
setAutoFillBackground(true);
setMinimumSize(600, 102);
setMaximumSize(600, 102);
setFrameShape(QFrame::Panel);
setFrameShadow(QFrame::Raised);
setLineWidth(2);




}

void Stripe_t::mousePressEvent(QMouseEvent *event)
{
std::cout << "Stripe_t::mousePressEvent" << std::endl;
Stripe_t *child = static_cast<Stripe_t*>(childAt(event->pos()));
if (!child)
return;

QPoint hotSpot = event->pos() - child->pos();

QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << QPoint(hotSpot);

QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-EFS", itemData);

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setHotSpot(hotSpot);

child->hide();

if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
child->close();
else
child->show();

}

void Stripe_t::dragEnterEvent(QDragEnterEvent *event)
{
}

void Stripe_t::dragMoveEvent(QDragMoveEvent *event)
{
}

void Stripe_t::dropEvent(QDropEvent *event)
{
}




That is all I have until now. My oppinion is to copy or move widgets of type Stripe_t from one Tablecell to anotherone in all Widgets of type StripeArea_t.