how to get the selected last clickable image in verticalbox layout uisng pyqt4
I am new to pyqt.I want display my clickable image in vertical layout.i got similar example from google but i don't know how to get the image from clickable label. So can anyone please help me to add the image in vbox if i selected another items then the first item will remove in the vertical box.i want to display the last clickable item only. Here i tried so many ways but i am getting the object.Thank you in advance.
Given below is the example:
from PyQt4 import QtCore, QtGui
import functools
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
highlight_dir = './floder1'
scrollArea = QtGui.QScrollArea(widgetResizable=True)
self.setCentralWidget(scrollArea)
content_widget = QtGui.QWidget()
scrollArea.setWidget(content_widget)
self._layout = QtGui.QGridLayout(content_widget)
self._it = QtCore.QDirIterator(highlight_dir)
self.vbox = QtGui.QVBoxLayout()
self.mainLayout = QtGui.QGridLayout()
self.mainLayout.addLayout(self.vbox,0,1)
self.mainLayout.addWidget(scrollArea,0,0)
self.setCentralWidget(QtGui.QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if not pixmap.isNull():
vlay = QtGui.QVBoxLayout()
self.label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
self.label_pixmap.mousePressEvent= functools.partial(self.item_discription,source_object=self.label_pixmap)
self.label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
print self.label_text.text()
vlay.addWidget(self.label_pixmap)
vlay.addWidget(self.label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def item_discription(self,event,source_object=None):
print self.label_text.text() #how to add clickable qlabel to vbox
self.vbox.addwidget(label_pixmap)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.setGeometry(500, 300, 900, 500)
w.show()
sys.exit(app.exec_())
python python-2.7 pyqt pyqt4
add a comment |
I am new to pyqt.I want display my clickable image in vertical layout.i got similar example from google but i don't know how to get the image from clickable label. So can anyone please help me to add the image in vbox if i selected another items then the first item will remove in the vertical box.i want to display the last clickable item only. Here i tried so many ways but i am getting the object.Thank you in advance.
Given below is the example:
from PyQt4 import QtCore, QtGui
import functools
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
highlight_dir = './floder1'
scrollArea = QtGui.QScrollArea(widgetResizable=True)
self.setCentralWidget(scrollArea)
content_widget = QtGui.QWidget()
scrollArea.setWidget(content_widget)
self._layout = QtGui.QGridLayout(content_widget)
self._it = QtCore.QDirIterator(highlight_dir)
self.vbox = QtGui.QVBoxLayout()
self.mainLayout = QtGui.QGridLayout()
self.mainLayout.addLayout(self.vbox,0,1)
self.mainLayout.addWidget(scrollArea,0,0)
self.setCentralWidget(QtGui.QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if not pixmap.isNull():
vlay = QtGui.QVBoxLayout()
self.label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
self.label_pixmap.mousePressEvent= functools.partial(self.item_discription,source_object=self.label_pixmap)
self.label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
print self.label_text.text()
vlay.addWidget(self.label_pixmap)
vlay.addWidget(self.label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def item_discription(self,event,source_object=None):
print self.label_text.text() #how to add clickable qlabel to vbox
self.vbox.addwidget(label_pixmap)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.setGeometry(500, 300, 900, 500)
w.show()
sys.exit(app.exec_())
python python-2.7 pyqt pyqt4
add a comment |
I am new to pyqt.I want display my clickable image in vertical layout.i got similar example from google but i don't know how to get the image from clickable label. So can anyone please help me to add the image in vbox if i selected another items then the first item will remove in the vertical box.i want to display the last clickable item only. Here i tried so many ways but i am getting the object.Thank you in advance.
Given below is the example:
from PyQt4 import QtCore, QtGui
import functools
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
highlight_dir = './floder1'
scrollArea = QtGui.QScrollArea(widgetResizable=True)
self.setCentralWidget(scrollArea)
content_widget = QtGui.QWidget()
scrollArea.setWidget(content_widget)
self._layout = QtGui.QGridLayout(content_widget)
self._it = QtCore.QDirIterator(highlight_dir)
self.vbox = QtGui.QVBoxLayout()
self.mainLayout = QtGui.QGridLayout()
self.mainLayout.addLayout(self.vbox,0,1)
self.mainLayout.addWidget(scrollArea,0,0)
self.setCentralWidget(QtGui.QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if not pixmap.isNull():
vlay = QtGui.QVBoxLayout()
self.label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
self.label_pixmap.mousePressEvent= functools.partial(self.item_discription,source_object=self.label_pixmap)
self.label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
print self.label_text.text()
vlay.addWidget(self.label_pixmap)
vlay.addWidget(self.label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def item_discription(self,event,source_object=None):
print self.label_text.text() #how to add clickable qlabel to vbox
self.vbox.addwidget(label_pixmap)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.setGeometry(500, 300, 900, 500)
w.show()
sys.exit(app.exec_())
python python-2.7 pyqt pyqt4
I am new to pyqt.I want display my clickable image in vertical layout.i got similar example from google but i don't know how to get the image from clickable label. So can anyone please help me to add the image in vbox if i selected another items then the first item will remove in the vertical box.i want to display the last clickable item only. Here i tried so many ways but i am getting the object.Thank you in advance.
Given below is the example:
from PyQt4 import QtCore, QtGui
import functools
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
highlight_dir = './floder1'
scrollArea = QtGui.QScrollArea(widgetResizable=True)
self.setCentralWidget(scrollArea)
content_widget = QtGui.QWidget()
scrollArea.setWidget(content_widget)
self._layout = QtGui.QGridLayout(content_widget)
self._it = QtCore.QDirIterator(highlight_dir)
self.vbox = QtGui.QVBoxLayout()
self.mainLayout = QtGui.QGridLayout()
self.mainLayout.addLayout(self.vbox,0,1)
self.mainLayout.addWidget(scrollArea,0,0)
self.setCentralWidget(QtGui.QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if not pixmap.isNull():
vlay = QtGui.QVBoxLayout()
self.label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
self.label_pixmap.mousePressEvent= functools.partial(self.item_discription,source_object=self.label_pixmap)
self.label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
print self.label_text.text()
vlay.addWidget(self.label_pixmap)
vlay.addWidget(self.label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def item_discription(self,event,source_object=None):
print self.label_text.text() #how to add clickable qlabel to vbox
self.vbox.addwidget(label_pixmap)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.setGeometry(500, 300, 900, 500)
w.show()
sys.exit(app.exec_())
python python-2.7 pyqt pyqt4
python python-2.7 pyqt pyqt4
edited Jan 2 at 4:13
gowthami
asked Jan 1 at 12:03
gowthamigowthami
658
658
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If you want to make a QLabel notify you when there is a click, the simplest thing is to write it as indicated in this answer.
On the other hand it is better that you use a QScrollArea as the basis of the QVBoxLayout since having a lot will not be able to visualize them all.
Finally I prefer to use QSplitter since it allows to change the size of the widgets easily.
from PyQt4 import QtCore, QtGui
from functools import partial
class ClickableLabel(QtGui.QLabel):
clicked = QtCore.pyqtSignal()
def mousePressEvent(self, event):
self.clicked.emit()
super(ClickableLabel, self).mousePressEvent(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
scrollArea_right = QtGui.QScrollArea(widgetResizable=True)
content_widget_right = QtGui.QWidget()
scrollArea_right.setWidget(content_widget_right)
self._vbox = QtGui.QVBoxLayout(content_widget_right)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(scrollArea_right)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
vlay = QtGui.QVBoxLayout()
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=text))
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap))
self._vbox.addLayout(vlay)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.showMaximized()
sys.exit(app.exec_())
Update:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
self._label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
self._label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
widget = QtGui.QWidget()
vbox = QtGui.QVBoxLayout(widget)
vbox.addWidget(self._label_pixmap)
vbox.addWidget(self._label_text)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(widget)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
self._label_pixmap.setPixmap(pixmap)
self._label_text.setText(text)
when i select the item it will display on vbox , again i selected another item then first item will remove from the vbox. i want to display the vbox in one item only
– gowthami
Jan 2 at 4:02
@gowthami In what part of your question does that indicate? Please edit your question so I can adapt my answer to your request.
– eyllanesc
Jan 2 at 4:07
@gowthami see my update.
– eyllanesc
Jan 2 at 4:14
ok got it thankyou sir
– gowthami
Jan 2 at 4:24
@gowthami Why do not you mark the other answers that help you as correct?
– eyllanesc
Jan 2 at 4:25
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995267%2fhow-to-get-the-selected-last-clickable-image-in-verticalbox-layout-uisng-pyqt4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to make a QLabel notify you when there is a click, the simplest thing is to write it as indicated in this answer.
On the other hand it is better that you use a QScrollArea as the basis of the QVBoxLayout since having a lot will not be able to visualize them all.
Finally I prefer to use QSplitter since it allows to change the size of the widgets easily.
from PyQt4 import QtCore, QtGui
from functools import partial
class ClickableLabel(QtGui.QLabel):
clicked = QtCore.pyqtSignal()
def mousePressEvent(self, event):
self.clicked.emit()
super(ClickableLabel, self).mousePressEvent(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
scrollArea_right = QtGui.QScrollArea(widgetResizable=True)
content_widget_right = QtGui.QWidget()
scrollArea_right.setWidget(content_widget_right)
self._vbox = QtGui.QVBoxLayout(content_widget_right)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(scrollArea_right)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
vlay = QtGui.QVBoxLayout()
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=text))
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap))
self._vbox.addLayout(vlay)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.showMaximized()
sys.exit(app.exec_())
Update:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
self._label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
self._label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
widget = QtGui.QWidget()
vbox = QtGui.QVBoxLayout(widget)
vbox.addWidget(self._label_pixmap)
vbox.addWidget(self._label_text)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(widget)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
self._label_pixmap.setPixmap(pixmap)
self._label_text.setText(text)
when i select the item it will display on vbox , again i selected another item then first item will remove from the vbox. i want to display the vbox in one item only
– gowthami
Jan 2 at 4:02
@gowthami In what part of your question does that indicate? Please edit your question so I can adapt my answer to your request.
– eyllanesc
Jan 2 at 4:07
@gowthami see my update.
– eyllanesc
Jan 2 at 4:14
ok got it thankyou sir
– gowthami
Jan 2 at 4:24
@gowthami Why do not you mark the other answers that help you as correct?
– eyllanesc
Jan 2 at 4:25
|
show 3 more comments
If you want to make a QLabel notify you when there is a click, the simplest thing is to write it as indicated in this answer.
On the other hand it is better that you use a QScrollArea as the basis of the QVBoxLayout since having a lot will not be able to visualize them all.
Finally I prefer to use QSplitter since it allows to change the size of the widgets easily.
from PyQt4 import QtCore, QtGui
from functools import partial
class ClickableLabel(QtGui.QLabel):
clicked = QtCore.pyqtSignal()
def mousePressEvent(self, event):
self.clicked.emit()
super(ClickableLabel, self).mousePressEvent(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
scrollArea_right = QtGui.QScrollArea(widgetResizable=True)
content_widget_right = QtGui.QWidget()
scrollArea_right.setWidget(content_widget_right)
self._vbox = QtGui.QVBoxLayout(content_widget_right)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(scrollArea_right)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
vlay = QtGui.QVBoxLayout()
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=text))
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap))
self._vbox.addLayout(vlay)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.showMaximized()
sys.exit(app.exec_())
Update:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
self._label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
self._label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
widget = QtGui.QWidget()
vbox = QtGui.QVBoxLayout(widget)
vbox.addWidget(self._label_pixmap)
vbox.addWidget(self._label_text)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(widget)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
self._label_pixmap.setPixmap(pixmap)
self._label_text.setText(text)
when i select the item it will display on vbox , again i selected another item then first item will remove from the vbox. i want to display the vbox in one item only
– gowthami
Jan 2 at 4:02
@gowthami In what part of your question does that indicate? Please edit your question so I can adapt my answer to your request.
– eyllanesc
Jan 2 at 4:07
@gowthami see my update.
– eyllanesc
Jan 2 at 4:14
ok got it thankyou sir
– gowthami
Jan 2 at 4:24
@gowthami Why do not you mark the other answers that help you as correct?
– eyllanesc
Jan 2 at 4:25
|
show 3 more comments
If you want to make a QLabel notify you when there is a click, the simplest thing is to write it as indicated in this answer.
On the other hand it is better that you use a QScrollArea as the basis of the QVBoxLayout since having a lot will not be able to visualize them all.
Finally I prefer to use QSplitter since it allows to change the size of the widgets easily.
from PyQt4 import QtCore, QtGui
from functools import partial
class ClickableLabel(QtGui.QLabel):
clicked = QtCore.pyqtSignal()
def mousePressEvent(self, event):
self.clicked.emit()
super(ClickableLabel, self).mousePressEvent(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
scrollArea_right = QtGui.QScrollArea(widgetResizable=True)
content_widget_right = QtGui.QWidget()
scrollArea_right.setWidget(content_widget_right)
self._vbox = QtGui.QVBoxLayout(content_widget_right)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(scrollArea_right)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
vlay = QtGui.QVBoxLayout()
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=text))
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap))
self._vbox.addLayout(vlay)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.showMaximized()
sys.exit(app.exec_())
Update:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
self._label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
self._label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
widget = QtGui.QWidget()
vbox = QtGui.QVBoxLayout(widget)
vbox.addWidget(self._label_pixmap)
vbox.addWidget(self._label_text)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(widget)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
self._label_pixmap.setPixmap(pixmap)
self._label_text.setText(text)
If you want to make a QLabel notify you when there is a click, the simplest thing is to write it as indicated in this answer.
On the other hand it is better that you use a QScrollArea as the basis of the QVBoxLayout since having a lot will not be able to visualize them all.
Finally I prefer to use QSplitter since it allows to change the size of the widgets easily.
from PyQt4 import QtCore, QtGui
from functools import partial
class ClickableLabel(QtGui.QLabel):
clicked = QtCore.pyqtSignal()
def mousePressEvent(self, event):
self.clicked.emit()
super(ClickableLabel, self).mousePressEvent(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
scrollArea_right = QtGui.QScrollArea(widgetResizable=True)
content_widget_right = QtGui.QWidget()
scrollArea_right.setWidget(content_widget_right)
self._vbox = QtGui.QVBoxLayout(content_widget_right)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(scrollArea_right)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
vlay = QtGui.QVBoxLayout()
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=text))
vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap))
self._vbox.addLayout(vlay)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.showMaximized()
sys.exit(app.exec_())
Update:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
content_widget_left = QtGui.QWidget()
scrollArea_left.setWidget(content_widget_left)
self._layout = QtGui.QGridLayout(content_widget_left)
self._label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
self._label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
widget = QtGui.QWidget()
vbox = QtGui.QVBoxLayout(widget)
vbox.addWidget(self._label_pixmap)
vbox.addWidget(self._label_text)
splitter = QtGui.QSplitter()
splitter.addWidget(scrollArea_left)
splitter.addWidget(widget)
splitter.setStretchFactor(0, 3)
splitter.setStretchFactor(1, 1)
self.setCentralWidget(splitter)
highlight_dir = './floder1'
self._it = QtCore.QDirIterator(highlight_dir)
self._row, self._col = 0, 0
QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
def load_image(self):
if self._it.hasNext():
pixmap = QtGui.QPixmap(self._it.next())
if pixmap.isNull():
return
vlay = QtGui.QVBoxLayout()
label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
vlay.addWidget(label_pixmap)
vlay.addWidget(label_text)
vlay.addStretch()
self._layout.addLayout(vlay, self._row, self._col)
self._col += 1
if self._col == 3:
self._row += 1
self._col = 0
def on_clicked(self, text, pixmap):
self._label_pixmap.setPixmap(pixmap)
self._label_text.setText(text)
edited Jan 2 at 4:26
answered Jan 1 at 19:04
eyllanesceyllanesc
81.4k103259
81.4k103259
when i select the item it will display on vbox , again i selected another item then first item will remove from the vbox. i want to display the vbox in one item only
– gowthami
Jan 2 at 4:02
@gowthami In what part of your question does that indicate? Please edit your question so I can adapt my answer to your request.
– eyllanesc
Jan 2 at 4:07
@gowthami see my update.
– eyllanesc
Jan 2 at 4:14
ok got it thankyou sir
– gowthami
Jan 2 at 4:24
@gowthami Why do not you mark the other answers that help you as correct?
– eyllanesc
Jan 2 at 4:25
|
show 3 more comments
when i select the item it will display on vbox , again i selected another item then first item will remove from the vbox. i want to display the vbox in one item only
– gowthami
Jan 2 at 4:02
@gowthami In what part of your question does that indicate? Please edit your question so I can adapt my answer to your request.
– eyllanesc
Jan 2 at 4:07
@gowthami see my update.
– eyllanesc
Jan 2 at 4:14
ok got it thankyou sir
– gowthami
Jan 2 at 4:24
@gowthami Why do not you mark the other answers that help you as correct?
– eyllanesc
Jan 2 at 4:25
when i select the item it will display on vbox , again i selected another item then first item will remove from the vbox. i want to display the vbox in one item only
– gowthami
Jan 2 at 4:02
when i select the item it will display on vbox , again i selected another item then first item will remove from the vbox. i want to display the vbox in one item only
– gowthami
Jan 2 at 4:02
@gowthami In what part of your question does that indicate? Please edit your question so I can adapt my answer to your request.
– eyllanesc
Jan 2 at 4:07
@gowthami In what part of your question does that indicate? Please edit your question so I can adapt my answer to your request.
– eyllanesc
Jan 2 at 4:07
@gowthami see my update.
– eyllanesc
Jan 2 at 4:14
@gowthami see my update.
– eyllanesc
Jan 2 at 4:14
ok got it thankyou sir
– gowthami
Jan 2 at 4:24
ok got it thankyou sir
– gowthami
Jan 2 at 4:24
@gowthami Why do not you mark the other answers that help you as correct?
– eyllanesc
Jan 2 at 4:25
@gowthami Why do not you mark the other answers that help you as correct?
– eyllanesc
Jan 2 at 4:25
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995267%2fhow-to-get-the-selected-last-clickable-image-in-verticalbox-layout-uisng-pyqt4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown