Results 1 to 2 of 2

Thread: pyqt scrollarea not resized after dragndrop

  1. #1
    Join Date
    Sep 2015
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default pyqt scrollarea not resized after dragndrop

    Hi there,
    i'm having trouble figuring how to update my scrollarea to reflect added or modified contents in my application.
    my program displays icons, i can dragndrop them.
    if i resize the window to be bigger and dragndrop one icon to the bottom,
    and then finally size back my window to it's original size,
    the scrollarea does not allow me to scroll to the bottom to see my icon.
    basicaly, once the app started, scrollarea dimension never change.
    how can i make the scrollarea, upon dragndrop, to update to new size ?
    it could be bigger like shown in the screenshot below,
    or smaller if all my icons are grouped in upper left corner for example..
    if the content fit in the window, i will not show the slider.

    so basicaly, to reproduce the problem:
    - take the code, run it, (all you need is two png icons)
    - resize the window to be a lot longer (Y axis)
    - dragndrop one of the icon far away (at least 300+ pixels away)
    - then resize window back to original size
    - then try to scroll to see the icon you dragndropped away.
    - you will not be able. because scrollarea is too small.
    - why? i mean i thought the layouts were there to communicate with widgets to transfer such info to them?


    here's a screenshot showing the problem,
    it's the same window, i just resize it, and dragndrop one icon at the bottom:

    Fi2FF.jpg

    Qt Code:
    1. #!/usr/bin/python3
    2.  
    3. from PyQt5.QtGui import *
    4. from PyQt5.QtCore import *
    5. from PyQt5.QtWidgets import *
    6. import sys
    7.  
    8.  
    9. class DragWidget(QFrame):
    10.  
    11. def __init__(self, parent=None):
    12. super(DragWidget, self).__init__(parent)
    13.  
    14. self.setMinimumSize(200, 200)
    15. self.setAcceptDrops(True)
    16.  
    17. test_icon1 = QLabel(self)
    18. test_icon1.setPixmap(QPixmap('./images/closeicon.png'))
    19. test_icon1.move(20, 20)
    20. test_icon1.show()
    21. test_icon1.setAttribute(Qt.WA_DeleteOnClose)
    22.  
    23. test_icon2 = QLabel(self)
    24. test_icon2.setPixmap(QPixmap('./images/openicon.png'))
    25. test_icon2.move(60, 20)
    26. test_icon2.show()
    27. test_icon2.setAttribute(Qt.WA_DeleteOnClose)
    28.  
    29. def dragEnterEvent(self, event):
    30. if event.mimeData().hasFormat('application/x-dnditemdata'):
    31. if event.source() == self:
    32. event.setDropAction(Qt.MoveAction)
    33. event.accept()
    34. else:
    35. event.acceptProposedAction()
    36. else:
    37. event.ignore()
    38.  
    39. dragMoveEvent = dragEnterEvent
    40.  
    41. def dropEvent(self, event):
    42. if event.mimeData().hasFormat('application/x-dnditemdata'):
    43. itemData = event.mimeData().data('application/x-dnditemdata')
    44. dataStream = QDataStream(itemData, QIODevice.ReadOnly)
    45.  
    46. pixmap = QPixmap()
    47. offset = QPoint()
    48. dataStream >> pixmap >> offset
    49.  
    50. newIcon = QLabel(self)
    51. newIcon.setPixmap(pixmap)
    52. newIcon.move(event.pos() - offset)
    53. newIcon.show()
    54. newIcon.setAttribute(Qt.WA_DeleteOnClose)
    55.  
    56. if event.source() == self:
    57. event.setDropAction(Qt.MoveAction)
    58. event.accept()
    59. else:
    60. event.acceptProposedAction()
    61. else:
    62. event.ignore()
    63.  
    64. def mousePressEvent(self, event):
    65. child = self.childAt(event.pos())
    66. if not child:
    67. return
    68.  
    69. pixmap = QPixmap(child.pixmap())
    70.  
    71. itemData = QByteArray()
    72. dataStream = QDataStream(itemData, QIODevice.WriteOnly)
    73. dataStream << pixmap << QPoint(event.pos() - child.pos())
    74.  
    75. mimeData = QMimeData()
    76. mimeData.setData('application/x-dnditemdata', itemData)
    77.  
    78. drag = QDrag(self)
    79. drag.setMimeData(mimeData)
    80. drag.setPixmap(pixmap)
    81. drag.setHotSpot(event.pos() - child.pos())
    82.  
    83. tempPixmap = QPixmap(pixmap)
    84. painter = QPainter()
    85. painter.begin(tempPixmap)
    86. painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127))
    87. painter.end()
    88.  
    89. child.setPixmap(tempPixmap)
    90. if drag.exec_(Qt.CopyAction | Qt.MoveAction) == Qt.MoveAction:
    91. child.close()
    92. else:
    93. child.show()
    94. child.setPixmap(pixmap)
    95.  
    96.  
    97. class Window(QWidget):
    98.  
    99. def __init__(self, parent=None):
    100. super(Window, self).__init__()
    101.  
    102. widget = QWidget()
    103. palette = QPalette()
    104. palette.setBrush(QPalette.Background, QBrush(QPixmap("images/pattern.png")))
    105. widget.setPalette(palette)
    106. layout = QVBoxLayout(self)
    107. layout.addWidget(DragWidget())
    108. widget.setLayout(layout)
    109. scroll = QScrollArea()
    110. scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
    111. scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    112. scroll.setWidgetResizable(True)
    113. scroll.setWidget(widget)
    114. vlayout = QVBoxLayout(self)
    115. vlayout.setContentsMargins(0, 0, 0, 0)
    116. vlayout.setSpacing(0)
    117. vlayout.addWidget(scroll)
    118. self.setLayout(vlayout)
    119. self.show()
    120.  
    121. if __name__ == '__main__':
    122. app = QApplication(sys.argv)
    123. window = Window('./')
    124. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2015
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default Re: pyqt scrollarea not resized after dragndrop

    i've found out:
    the dropEvent() method of DragWidget() class needed to be
    updated to get the dropped icon X and Y values and pass them to self.setMinimuSize()
    then scrollArea would know about the new dimensions.

    Qt Code:
    1. if newIcon.y()+32 > self.minimumHeight():
    2. self.setMinimumHeight(newIcon.y()+32)
    3.  
    4. if newIcon.x()+32 > self.minimumWidth():
    5. self.setMinimumWidth(newIcon.x()+32)
    To copy to clipboard, switch view to plain text mode 

    so if the icon new position is greater than
    the minimumSize (minimumWidth and minimumHeight),
    then add the offset to self.minimumSize

Similar Threads

  1. Replies: 0
    Last Post: 29th January 2015, 11:55
  2. Basic QTreeView DragNDrop Question
    By iraytrace in forum Newbie
    Replies: 1
    Last Post: 7th October 2009, 14:15
  3. QGraphicsItem & dragndrop
    By prashant in forum Qt Programming
    Replies: 0
    Last Post: 22nd September 2009, 20:10
  4. dragNdrop between QTreeWidget and QTextEdit
    By jh in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2006, 15:36
  5. QTreeView & DragNDrop
    By itan in forum Qt Programming
    Replies: 6
    Last Post: 10th August 2006, 00:33

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.