Results 1 to 3 of 3

Thread: dragging cellwidgets in tablewidgets

  1. #1
    Join Date
    May 2009
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default dragging cellwidgets in tablewidgets

    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???

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dragging cellwidgets in tablewidgets

    can you share some details of implementation..some code would be useful..

  3. #3
    Join Date
    May 2009
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dragging cellwidgets in tablewidgets

    Well the application consists 3 classes and I will share all I've done till now.

    Mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QCloseEvent>
    6. #include "ui_mainwindow.h"
    7.  
    8. class StripeArea_t;
    9. class Stripe_t;
    10.  
    11. class MainWindow : public QMainWindow, public Ui_MainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20.  
    21. StripeArea_t* LeftHandoverArea;
    22. StripeArea_t* LeftStripeArea;
    23. StripeArea_t* RightStripeArea;
    24. StripeArea_t* RightHandoverArea;
    25.  
    26. Stripe_t* Stripe1;
    27. Stripe_t* Stripe2;
    28.  
    29. bool CanClose;
    30.  
    31. void createStripeAreas();
    32.  
    33. void closeEvent(QCloseEvent*);
    34. };
    35.  
    36. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    Mainwindow.cpp

    Qt Code:
    1. #include "Mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "StripeArea.h"
    4. #include "Stripe.h"
    5. #include <QApplication>
    6. #include <QCleanlooksStyle>
    7.  
    8. MainWindow::MainWindow(QWidget *parent)
    9. : QMainWindow(parent)
    10. {
    11. setupUi(this);
    12. createStripeAreas();
    13. // showFullScreen();
    14.  
    15.  
    16. connect(actionClose, SIGNAL(triggered()), this, SLOT(close()));
    17.  
    18. LeftStripeArea->setColumnCount(1);
    19. LeftStripeArea->setRowCount(1);
    20. LeftStripeArea->setRowHeight(0,105);
    21. Stripe1 = new Stripe_t(LeftStripeArea);
    22. LeftStripeArea->setRowCount(2);
    23. LeftStripeArea->setRowHeight(1,105);
    24. LeftStripeArea->setCellWidget(0, 0, Stripe1);
    25. Stripe2 = new Stripe_t(LeftStripeArea);
    26. LeftStripeArea->setCellWidget(1, 0, Stripe2);
    27. //
    28.  
    29. CanClose = true;
    30. }
    31.  
    32. MainWindow::~MainWindow()
    33. {
    34. }
    35.  
    36.  
    37. void MainWindow::createStripeAreas()
    38. {
    39.  
    40. LeftHandoverArea = new StripeArea_t(LeftWidgetContents, StripeArea_t::HANDOVER);
    41. LeftWidgetLayout->addWidget(LeftHandoverArea , 0, 0, 1, 1);
    42.  
    43.  
    44. LeftStripeArea = new StripeArea_t(this, StripeArea_t::STRIPE);
    45. MainLayout->addWidget(LeftStripeArea , 1, 1, 1, 1);
    46.  
    47.  
    48.  
    49. RightStripeArea = new StripeArea_t(this, StripeArea_t::STRIPE);
    50. MainLayout->addWidget(RightStripeArea, 1, 2, 1, 1);
    51.  
    52.  
    53.  
    54. RightHandoverArea = new StripeArea_t(RightWidgetContents, StripeArea_t::HANDOVER);
    55. RightWidgetLayout->addWidget(RightHandoverArea, 0, 1);
    56. }
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
    64. void MainWindow::closeEvent(QCloseEvent* CE)
    65. {
    66. if(CanClose)
    67. {
    68. CE->accept();
    69. }
    70. else
    71. {
    72. CE->ignore();
    73. }
    74. }
    To copy to clipboard, switch view to plain text mode 


    StripeArea.h

    Qt Code:
    1. #ifndef STRIPEAREA_H
    2. #define STRIPEAREA_H
    3.  
    4. #include <QTableWidget>
    5. #include <QWidget>
    6. #include <QtGui/QHeaderView>
    7.  
    8.  
    9.  
    10. class StripeArea_t : public QTableWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. enum AreaType{NOAREA, STRIPE, HANDOVER};
    15.  
    16.  
    17. StripeArea_t(QWidget* _Parent = 0, AreaType _Area = NOAREA);
    18.  
    19.  
    20. protected:
    21. void dragEnterEvent(QDragEnterEvent *event);
    22. void dragMoveEvent(QDragMoveEvent *event);
    23. void dropEvent(QDropEvent *event);
    24. void mousePressEvent(QMouseEvent *event);
    25. };
    26.  
    27. #endif // STRIPEAREA_H
    To copy to clipboard, switch view to plain text mode 

    StripeArea.cpp
    Qt Code:
    1. #include <QMouseEvent>
    2.  
    3.  
    4. #include "StripeArea.h"
    5. #include "Stripe.h"
    6. #include <iostream>
    7.  
    8.  
    9.  
    10. StripeArea_t::StripeArea_t(QWidget* _Parent, AreaType _Area)
    11. : QTableWidget(_Parent)
    12. {
    13. QPalette palette;
    14. QBrush brush;
    15.  
    16. switch(_Area)
    17. {
    18. case HANDOVER:
    19. {
    20. brush.setColor(QColor(255, 236, 188, 255));
    21. break;
    22. }
    23. case STRIPE:
    24. {
    25. brush.setColor(QColor(171, 205, 255, 255));
    26. break;
    27. }
    28. default:
    29. {
    30. break;
    31. }
    32. }
    33.  
    34. brush.setStyle(Qt::SolidPattern);
    35. palette.setBrush(QPalette::Active, QPalette::Base, brush);
    36. palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
    37. setPalette(palette);
    38. setAutoFillBackground(true);
    39. setFrameShape(QFrame::StyledPanel);
    40. setFrameShadow(QFrame::Raised);
    41. setAutoFillBackground(true);
    42. horizontalHeader()->setVisible(false);
    43. verticalHeader()->setVisible(false);
    44. }
    45.  
    46.  
    47. void StripeArea_t::mousePressEvent(QMouseEvent *event)
    48. {
    49. Stripe_t *child = static_cast<Stripe_t*>(childAt(event->pos()));
    50. if (!child)
    51. return;
    52.  
    53. QPoint hotSpot = event->pos() - child->pos();
    54.  
    55. QByteArray itemData;
    56. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    57. dataStream << QPoint(hotSpot);
    58.  
    59. QMimeData *mimeData = new QMimeData;
    60. mimeData->setData("application/x-EFS", itemData);
    61.  
    62. QDrag *drag = new QDrag(this);
    63. drag->setMimeData(mimeData);
    64. drag->setHotSpot(hotSpot);
    65.  
    66. child->hide();
    67.  
    68. if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
    69. child->close();
    70. else
    71. child->show();
    72.  
    73. }
    74.  
    75. void StripeArea_t::dragEnterEvent(QDragEnterEvent *event)
    76. {
    77. }
    78.  
    79. void StripeArea_t::dragMoveEvent(QDragMoveEvent *event)
    80. {
    81. }
    82.  
    83. void StripeArea_t::dropEvent(QDropEvent *event)
    84. {
    85. }
    To copy to clipboard, switch view to plain text mode 

    Stripe.h
    Qt Code:
    1. #ifndef STRIPE_H
    2. #define STRIPE_H
    3.  
    4. #include <QFrame>
    5.  
    6. class Stripe_t : public QFrame
    7. {
    8. Q_OBJECT
    9. public:
    10. Stripe_t(QWidget* _parent = 0);
    11.  
    12. protected:
    13. void dragEnterEvent(QDragEnterEvent *event);
    14. void dragMoveEvent(QDragMoveEvent *event);
    15. void dropEvent(QDropEvent *event);
    16. void mousePressEvent(QMouseEvent *event);
    17.  
    18. };
    19.  
    20. #endif // STRIPE_H
    To copy to clipboard, switch view to plain text mode 

    Stripe.cpp
    Qt Code:
    1. #include <QMouseEvent>
    2.  
    3. #include "Stripe.h"
    4. #include <iostream>
    5.  
    6.  
    7. Stripe_t::Stripe_t(QWidget* _Parent)
    8. : QFrame(_Parent)
    9. {
    10. QPalette palette;
    11. QBrush brush;
    12. QBrush brush1;
    13. brush.setColor(QColor(238, 238, 238, 255));
    14. brush.setColor(QColor(255, 255, 255, 255));
    15. brush.setStyle(Qt::SolidPattern);
    16. palette.setBrush(QPalette::Active, QPalette::Base, brush);
    17. brush1.setColor(QColor(171, 205, 255, 255));
    18. brush1.setStyle(Qt::SolidPattern);
    19. palette.setBrush(QPalette::Active, QPalette::Window, brush1);
    20. palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
    21. palette.setBrush(QPalette::Inactive, QPalette::Window, brush1);
    22. palette.setBrush(QPalette::Disabled, QPalette::Base, brush1);
    23. palette.setBrush(QPalette::Disabled, QPalette::Window, brush1);
    24. setPalette(palette);
    25. setAutoFillBackground(true);
    26. setMinimumSize(600, 102);
    27. setMaximumSize(600, 102);
    28. setFrameShape(QFrame::Panel);
    29. setFrameShadow(QFrame::Raised);
    30. setLineWidth(2);
    31.  
    32.  
    33.  
    34.  
    35. }
    36.  
    37. void Stripe_t::mousePressEvent(QMouseEvent *event)
    38. {
    39. std::cout << "Stripe_t::mousePressEvent" << std::endl;
    40. Stripe_t *child = static_cast<Stripe_t*>(childAt(event->pos()));
    41. if (!child)
    42. return;
    43.  
    44. QPoint hotSpot = event->pos() - child->pos();
    45.  
    46. QByteArray itemData;
    47. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    48. dataStream << QPoint(hotSpot);
    49.  
    50. QMimeData *mimeData = new QMimeData;
    51. mimeData->setData("application/x-EFS", itemData);
    52.  
    53. QDrag *drag = new QDrag(this);
    54. drag->setMimeData(mimeData);
    55. drag->setHotSpot(hotSpot);
    56.  
    57. child->hide();
    58.  
    59. if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
    60. child->close();
    61. else
    62. child->show();
    63.  
    64. }
    65.  
    66. void Stripe_t::dragEnterEvent(QDragEnterEvent *event)
    67. {
    68. }
    69.  
    70. void Stripe_t::dragMoveEvent(QDragMoveEvent *event)
    71. {
    72. }
    73.  
    74. void Stripe_t::dropEvent(QDropEvent *event)
    75. {
    76. }
    To copy to clipboard, switch view to plain text mode 

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.