Results 1 to 3 of 3

Thread: Drop on table and widget

  1. #1
    Join Date
    Oct 2010
    Posts
    19
    Qt products
    Qt4

    Default Drop on table and widget

    Hi! I have Widget MainWindow and tableWidget in MainWindow. tableWidget is placed in the center of MainWindow. I need drop audio-files on Main Window. If i drop files to MainWindow then do such function, if i drop files on table - do another function. Can you help me?

    i try to realize drop on mainWindow:
    in header:
    Qt Code:
    1. void dragEnterEvent (QDragEnterEvent *pe){
    2. if (pe->mimeData()->hasFormat("text/uri-list")){
    3. pe->acceptProposedAction();
    4. }
    5. }
    6. void dropEvent (QDropEvent* pe);
    To copy to clipboard, switch view to plain text mode 

    in constructor

    Qt Code:
    1. setAcceptDrops(true);
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void MainWindow::dropEvent(QDropEvent *pe){
    2. QList<QUrl> urlList=pe->mimeData()->urls();
    3. foreach (QUrl url, urlList ){
    4. str<<url.toString();
    5. QMessageBox::information(this,"yeah",url.toString());
    6. }
    7. // Some function with str
    8. }
    To copy to clipboard, switch view to plain text mode 

    But i have crash on drop.

  2. #2
    Join Date
    Nov 2010
    Location
    Belgium
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drop on table and widget

    Comment out lines until the crash goes away. That way you know where things are going wrong...

    My drop event (to add single files) looks as follows:

    Qt Code:
    1. void dropEvent(QDropEvent* event) {
    2. QString filePath;
    3. if (event->mimeData()->hasFormat( "text/uri-list" )) {
    4. QList<QUrl> urls = event->mimeData()->urls();
    5. if (urls.isEmpty()) {
    6. return;
    7. }
    8. if(urls.size()>1) {
    9. return;
    10. }
    11. filePath = urls.first().toLocalFile();
    12. if(filePath.isEmpty()) {
    13. return;
    14. }
    15. }
    16. // do something with filePath
    17. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drop on table and widget

    you could use the model/view framework built in to qt to accept the dropped data into a model. you would have to subclass QStandardItemModel and override the drag/drop functions, then you just need to tell the view to use that model. here is some code modified from the Bangarang project...

    Qt Code:
    1. bool MusicItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
    2. {
    3. if (action == Qt::IgnoreAction)
    4. return true;
    5.  
    6. if (!data->hasFormat("text/uri-list"))
    7. return false;
    8.  
    9. if (column > 0)
    10. return false;
    11.  
    12. int beginRow;
    13.  
    14. if (row != -1) {
    15. beginRow = row;
    16. } else if (parent.isValid()) {
    17. beginRow = parent.row();
    18. } else {
    19. beginRow = rowCount(QModelIndex());
    20. }
    21.  
    22. QList<QUrl> urls = data->urls();
    23.  
    24. bool internalMove = false;
    25. QStringList rowsToMove;
    26. if (data->text().startsWith("Row:")) {
    27. rowsToMove = data->text().split(",", QString::SkipEmptyParts);
    28. internalMove = true;
    29. }
    30.  
    31. QList<MusicItem> itemsInserted;
    32. int insertionRow = beginRow;
    33. for (int i = 0; i < urls.count(); i++) {
    34. if (internalMove) {
    35. QString rowEntry = rowsToMove.at(i);
    36. int rowToMove = rowEntry.remove("Row:").toInt();
    37. MusicItem item = itemAt(rowToMove);
    38. itemsInserted << item;
    39. QList<QStandardItem *> rowItems = getRowData(item);
    40. insertRow(insertionRow, rowItems);
    41. insertionRow = insertionRow + 1;
    42. } else {
    43. QString url = urls.at(i).toString();
    44. if (QFile::exists(url)) {
    45. MusicItem item = Convenience::itemFromUrl(url);
    46. itemsInserted << item;
    47. QList<QStandardItem *> rowItems = getRowData(item);
    48. insertRow(insertionRow, rowItems);
    49. insertionRow = insertionRow + 1;
    50. }
    51. }
    52. }
    53.  
    54. insertionRow = beginRow;
    55. foreach (MusicItem item, itemsInserted) {
    56. int i = rowFromItemUrl(item.Url);
    57. m_musiclist.insert(insertionRow, item);
    58. m_urllist.insert(insertionRow, item.Url);
    59. if (i != -1){
    60. m_musiclist.removeAt(i);
    61. m_urllist.removeAt(i);
    62. }
    63. insertionRow = insertionRow + 1;
    64.  
    65. }
    66. return true;
    67. }
    68.  
    69. Qt::ItemFlags MusicItemModel::flags(const QModelIndex &index) const
    70. {
    71. Qt::ItemFlags defaultFlags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    72.  
    73. if (index.isValid()) {
    74. return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
    75. } else {
    76. return Qt::ItemIsDropEnabled | defaultFlags;
    77. }
    78. }
    79.  
    80. QMimeData* MusicItemModel::mimeData(const QModelIndexList &indexes) const
    81. {
    82. QMimeData *mimeData = new QMimeData();
    83. QList<QUrl> urls;
    84. QString indexList;
    85. foreach (QModelIndex index, indexes) {
    86. if (index.isValid() && index.column() != 1) {
    87. QUrl url = QUrl(data(index, MusicItem::UrlRole).toString());
    88. urls << url;
    89. indexList += QString("Row:%1,").arg(index.row());
    90. }
    91. }
    92.  
    93. mimeData->setUrls(urls);
    94. mimeData->setText(indexList);
    95. return mimeData;
    96. }
    97.  
    98. QStringList MusicItemModel::mimeTypes() const
    99. {
    100. QStringList types;
    101. types << "text/uri-list" << "text/plain";
    102. return types;
    103. }
    104.  
    105. Qt::DropActions MusicItemModel::supportedDropActions() const
    106. {
    107. return Qt::MoveAction;
    108. }
    To copy to clipboard, switch view to plain text mode 

    hope it helps

Similar Threads

  1. Column/Row no of ComboBox Widget in Table
    By ankurjain in forum Qt Programming
    Replies: 13
    Last Post: 6th December 2011, 11:03
  2. select more than one row in table widget using pyqt
    By parkourpenner in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2009, 18:20
  3. Getting an item from an empty table widget
    By ProTonS in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2009, 02:24
  4. Table Widget Vs. Table View
    By winston2020 in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2008, 09:56
  5. customizing table widget
    By krishna.bv in forum Qt Programming
    Replies: 1
    Last Post: 25th January 2007, 13:43

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.