How can I split a widget and add some contents on it in PyQt4?
Dear Python experts!!!
Could you refer me some books for PyQt in which the documentation would be clear(other than Dive into python and Rapid GUI development in python)?
sindhujavenkat 0 Newbie Poster
woooee 814 Nearly a Posting Maven
You don't actually split a widget, you use 2 widgets and pass whatever data back and forth. The following code is not exactly what you want but does show how to pass data from one widget to another. When you click on the 'Edit' button the item you selected is passed to the pop up box. The 'edit' function is the function that does this using getText() and setText(). And you could remove the code for the other buttons if that makes the example easier to understand.
Some general references:
http://www.zetcode.com/tutorials/pyqt4/
http://www.diotavelli.net/PyQtWiki/Tutorials
http://wiki.python.org/moin/PyQt4
#!/usr/bin/env python
# Copyright (c) 2007-8 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 2 of the License, or
# version 3 of the License, or (at your option) any later version. It is
# provided for educational purposes and is distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
MAC = "qt_mac_set_native_menubar" in dir()
class StringListDlg(QDialog):
def __init__(self, name, stringlist=None, parent=None):
super(StringListDlg, self).__init__(parent)
self.name = name
self.listWidget = QListWidget()
self.stringlist = QStringList()
if stringlist is not None:
self.listWidget.addItems(stringlist)
self.listWidget.setCurrentRow(0)
self.stringlist = stringlist[:]
buttonLayout = QVBoxLayout()
for text, slot in (("&Add...", self.add),
("&Edit...", self.edit),
("&Remove...", self.remove),
("&Up", self.up),
("&Down", self.down),
("&Sort", self.listWidget.sortItems),
("Close", self.accept)):
button = QPushButton(text)
if not MAC:
button.setFocusPolicy(Qt.NoFocus)
if text == "Close":
buttonLayout.addStretch()
buttonLayout.addWidget(button)
self.connect(button, SIGNAL("clicked()"), slot)
layout = QHBoxLayout()
layout.addWidget(self.listWidget)
layout.addLayout(buttonLayout)
self.setLayout(layout)
self.setWindowTitle("Edit %s List" % self.name)
def add(self):
row = self.listWidget.currentRow()
title = "Add %s" % self.name
string, ok = QInputDialog.getText(self, title, title)
if ok and not string.isEmpty():
self.listWidget.insertItem(row, string)
def edit(self):
row = self.listWidget.currentRow()
item = self.listWidget.item(row)
if item is not None:
title = "Edit %s" % self.name
string, ok = QInputDialog.getText(self, title, title,
QLineEdit.Normal, item.text())
if ok and not string.isEmpty():
item.setText(string)
def remove(self):
row = self.listWidget.currentRow()
item = self.listWidget.item(row)
if item is None:
return
reply = QMessageBox.question(self, "Remove %s" % self.name,
"Remove %s `%s'?" % (
self.name, unicode(item.text())),
QMessageBox.Yes|QMessageBox.No)
if reply == QMessageBox.Yes:
item = self.listWidget.takeItem(row)
del item
def up(self):
row = self.listWidget.currentRow()
if row >= 1:
thisItem = self.listWidget.item(row)
prevItem = self.listWidget.item(row - 1)
text = thisItem.text()
thisItem.setText(prevItem.text())
prevItem.setText(text)
self.listWidget.setCurrentItem(prevItem)
def down(self):
row = self.listWidget.currentRow()
if row < self.listWidget.count() - 1:
thisItem = self.listWidget.item(row)
nextItem = self.listWidget.item(row + 1)
text = thisItem.text()
thisItem.setText(nextItem.text())
nextItem.setText(text)
self.listWidget.setCurrentItem(nextItem)
def reject(self):
self.accept()
def accept(self):
self.stringlist = QStringList()
for row in range(self.listWidget.count()):
self.stringlist.append(self.listWidget.item(row).text())
self.emit(SIGNAL("acceptedList(QStringList)"), self.stringlist)
QDialog.accept(self)
if __name__ == "__main__":
fruit = ["Banana", "Apple", "Elderberry", "Clementine", "Fig",
"Guava", "Mango", "Honeydew Melon", "Date", "Watermelon",
"Tangerine", "Ugli Fruit", "Juniperberry", "Kiwi",
"Lemon", "Nectarine", "Plum", "Raspberry", "Strawberry",
"Orange"]
app = QApplication(sys.argv)
form = StringListDlg("Fruit", fruit)
form.exec_()
print "\n".join([unicode(x) for x in form.stringlist])
Edited by woooee because: n/a
sindhujavenkat 0 Newbie Poster
You don't actually split a widget, you use 2 widgets and pass whatever data back and forth. The following code is not exactly what you want but does show how to pass data from one widget to another. When you click on the 'Edit' button the item you selected is passed to the pop up box. The 'edit' function is the function that does this using getText() and setText(). And you could remove the code for the other buttons if that makes the example easier to understand.
Some general references:
http://www.zetcode.com/tutorials/pyqt4/
http://www.diotavelli.net/PyQtWiki/Tutorials
http://wiki.python.org/moin/PyQt4#!/usr/bin/env python # Copyright (c) 2007-8 Qtrac Ltd. All rights reserved. # This program or module is free software: you can redistribute it and/or # modify it under the terms of the GNU General Public License as published # by the Free Software Foundation, either version 2 of the License, or # version 3 of the License, or (at your option) any later version. It is # provided for educational purposes and is distributed in the hope that # it will be useful, but WITHOUT ANY WARRANTY; without even the implied # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See # the GNU General Public License for more details. import sys from PyQt4.QtCore import * from PyQt4.QtGui import * MAC = "qt_mac_set_native_menubar" in dir() class StringListDlg(QDialog): def __init__(self, name, stringlist=None, parent=None): super(StringListDlg, self).__init__(parent) self.name = name self.listWidget = QListWidget() self.stringlist = QStringList() if stringlist is not None: self.listWidget.addItems(stringlist) self.listWidget.setCurrentRow(0) self.stringlist = stringlist[:] buttonLayout = QVBoxLayout() for text, slot in (("&Add...", self.add), ("&Edit...", self.edit), ("&Remove...", self.remove), ("&Up", self.up), ("&Down", self.down), ("&Sort", self.listWidget.sortItems), ("Close", self.accept)): button = QPushButton(text) if not MAC: button.setFocusPolicy(Qt.NoFocus) if text == "Close": buttonLayout.addStretch() buttonLayout.addWidget(button) self.connect(button, SIGNAL("clicked()"), slot) layout = QHBoxLayout() layout.addWidget(self.listWidget) layout.addLayout(buttonLayout) self.setLayout(layout) self.setWindowTitle("Edit %s List" % self.name) def add(self): row = self.listWidget.currentRow() title = "Add %s" % self.name string, ok = QInputDialog.getText(self, title, title) if ok and not string.isEmpty(): self.listWidget.insertItem(row, string) def edit(self): row = self.listWidget.currentRow() item = self.listWidget.item(row) if item is not None: title = "Edit %s" % self.name string, ok = QInputDialog.getText(self, title, title, QLineEdit.Normal, item.text()) if ok and not string.isEmpty(): item.setText(string) def remove(self): row = self.listWidget.currentRow() item = self.listWidget.item(row) if item is None: return reply = QMessageBox.question(self, "Remove %s" % self.name, "Remove %s `%s'?" % ( self.name, unicode(item.text())), QMessageBox.Yes|QMessageBox.No) if reply == QMessageBox.Yes: item = self.listWidget.takeItem(row) del item def up(self): row = self.listWidget.currentRow() if row >= 1: thisItem = self.listWidget.item(row) prevItem = self.listWidget.item(row - 1) text = thisItem.text() thisItem.setText(prevItem.text()) prevItem.setText(text) self.listWidget.setCurrentItem(prevItem) def down(self): row = self.listWidget.currentRow() if row < self.listWidget.count() - 1: thisItem = self.listWidget.item(row) nextItem = self.listWidget.item(row + 1) text = thisItem.text() thisItem.setText(nextItem.text()) nextItem.setText(text) self.listWidget.setCurrentItem(nextItem) def reject(self): self.accept() def accept(self): self.stringlist = QStringList() for row in range(self.listWidget.count()): self.stringlist.append(self.listWidget.item(row).text()) self.emit(SIGNAL("acceptedList(QStringList)"), self.stringlist) QDialog.accept(self) if __name__ == "__main__": fruit = ["Banana", "Apple", "Elderberry", "Clementine", "Fig", "Guava", "Mango", "Honeydew Melon", "Date", "Watermelon", "Tangerine", "Ugli Fruit", "Juniperberry", "Kiwi", "Lemon", "Nectarine", "Plum", "Raspberry", "Strawberry", "Orange"] app = QApplication(sys.argv) form = StringListDlg("Fruit", fruit) form.exec_() print "\n".join([unicode(x) for x in form.stringlist])
Thaks a lot
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.