I'm using pyqt4 with python 2.7 and I have a list view widget that I can't add items to it
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'add_category.ui'
#
# Created: Mon Mar 19 23:22:30 2018
# by: PyQt4 UI code generator 4.10
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog1(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(608, 460)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/media/media/in.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
self.gridLayout = QtGui.QGridLayout(Dialog)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setIcon(icon)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout.addWidget(self.pushButton, 0, 6, 1, 1)
self.label = QtGui.QLabel(Dialog)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Adobe Arabic"))
font.setPointSize(24)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.listView = QtGui.QListView(Dialog)
self.listView.setObjectName(_fromUtf8("listView"))
entries = ['one','two', 'three']
for i in entries:
item = QtGui.QListView(i)
self.listView.addItem(item)
self.gridLayout.addWidget(self.listView, 1, 0, 1, 2)
self.pushButton_2 = QtGui.QPushButton(Dialog)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8(":/media/media/ok.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_2.setIcon(icon1)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
def go_back(self):
Dialog.hide()
self.pushButton_2.clicked.connect(go_back)
self.gridLayout.addWidget(self.pushButton_2, 2, 1, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "إضافة فئة", None))
self.lineEdit.setPlaceholderText(_translate("Dialog", "هنا يكتب الاسم الفئة الجديدة", None))
self.pushButton.setText(_translate("Dialog", "إضافة", None))
self.label.setText(_translate("Dialog", "إسـم الفئة الجديدة", None))
self.pushButton_2.setText(_translate("Dialog", "موافق", None))
import resrcs
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog1()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
As you guys can see i used
entries = ['one','two', 'three']
for i in entries:
item = QtGui.QListView(i)
self.listView.addItem(item)
But it gave me an error that is talking about arguments and data types:
Traceback (most recent call last):
File "C:\python\townoftechwarehouse\add_category.py", line 84, in <module>
ui.setupUi(Dialog)
File "C:\python\townoftechwarehouse\add_category.py", line 54, in setupUi
item = QtGui.QListView(i)
TypeError: QListView(QWidget parent=None): argument 1 has unexpected type 'str'
[Finished in 1.7s]
Also, is it right to use ListView here or I should use listwidget?
In general, what is the difference between both !!
QListWidget is a class of higher level that makes the developer can handle it easily, for example QListWidget has a model of type QStantandardItemModel that can not be accessed, in addition to built the QListWidgetItem to handle the data, as you have seen is simple add data through functions like addItem() or addItems().
On the other hand QListView, from which QListWidget inherits, is of lower level, where you can customize many things, using custom models, etc.
You can use both:
QListView
self.listView = QtGui.QListView(Dialog)
self.listView.setObjectName(_fromUtf8("listView"))
entries = ['one', 'two', 'three']
model = QtGui.QStandardItemModel()
self.listView.setModel(model)
for i in entries:
item = QtGui.QStandardItem(i)
model.appendRow(item)
self.gridLayout.addWidget(self.listView, 1, 0, 1, 2)
QListWidget
self.listwidget = QtGui.QListWidget(Dialog)
entries = ['one', 'two', 'three']
self.listwidget.addItems(entries)
self.gridLayout.addWidget(self.listwidget, 1, 0, 1, 2)
Related
I made a GUI in Qt Designer and I have 144 check boxes. I want to connect all of them with a QPushButton to check and uncheck all of them.
How can I do that?
They are all inside of a QGridLayout.
They are named following a "trend", so I tried to write their names in a list and call each item of the list to check, but I did not manage.
This example is more or less like what I have
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'check.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.checkBox_2 = QtGui.QCheckBox(Form)
self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
self.gridLayout.addWidget(self.checkBox_2, 2, 0, 1, 2)
self.checkBox = QtGui.QCheckBox(Form)
self.checkBox.setObjectName(_fromUtf8("checkBox"))
self.gridLayout.addWidget(self.checkBox, 2, 2, 1, 1)
self.checkBox_3 = QtGui.QCheckBox(Form)
self.checkBox_3.setObjectName(_fromUtf8("checkBox_3"))
self.gridLayout.addWidget(self.checkBox_3, 3, 0, 1, 2)
self.checkBox_4 = QtGui.QCheckBox(Form)
self.checkBox_4.setObjectName(_fromUtf8("checkBox_4"))
self.gridLayout.addWidget(self.checkBox_4, 3, 2, 1, 1)
self.checkBox_5 = QtGui.QCheckBox(Form)
self.checkBox_5.setObjectName(_fromUtf8("checkBox_5"))
self.gridLayout.addWidget(self.checkBox_5, 1, 2, 1, 1)
self.checkBox_6 = QtGui.QCheckBox(Form)
self.checkBox_6.setObjectName(_fromUtf8("checkBox_6"))
self.gridLayout.addWidget(self.checkBox_6, 1, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.checkBox_2.setText(_translate("Form", "CheckBox", None))
self.checkBox.setText(_translate("Form", "CheckBox", None))
self.checkBox_3.setText(_translate("Form", "CheckBox", None))
self.checkBox_4.setText(_translate("Form", "CheckBox", None))
self.checkBox_5.setText(_translate("Form", "CheckBox", None))
self.checkBox_6.setText(_translate("Form", "CheckBox", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
I want to make a button to check and uncheck all of them without having to type ao the connects
It is tedious to have to make many connections, but as you say you can create an object list with findChildren, but first add a button to the design.
class Ui_Form(object):
def setupUi(self, Form):
...
self.gridLayout.addWidget(self.checkBox_6, 1, 0, 1, 1)
self.btn = QtGui.QPushButton("Check", Form)
self.gridLayout.addWidget(self.btn, 4, 0, 1, 3)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
...
Then we implement the logic in another class and we use findChildren to obtain the QCheckBox:
class Widget(QtGui.QWidget, Ui_Form):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.checkBoxList = self.findChildren(QtGui.QCheckBox)
self.btn.clicked.connect(self.onClicked)
def onClicked(self):
state = self.sender().text() == "Check"
for btn in self.checkBoxList:
btn.setChecked(state)
self.sender().setText("Uncheck" if state else "Check")
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = Widget()
Form.show()
sys.exit(app.exec_())
I am developing an application using PyQt4 and Qt4Designer, I have designed from designer and generated python code. As you can see in the below image, when I am adding first row, it's occupying whole layout space, but later rows are of fixed width.
Image showing the actual problem
Here's my code...
main.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'pyqt.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(983, 600)
MainWindow.setMinimumSize(QtCore.QSize(900, 600))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(100, 90, 675, 441))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, -1, -1, -1)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tableWidget = QtGui.QTableWidget(self.gridLayoutWidget)
self.tableWidget.setMinimumSize(QtCore.QSize(589, 439))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.tableWidget.horizontalHeader().setCascadingSectionResizes(False)
self.tableWidget.verticalHeader().setStretchLastSection(True)
self.gridLayout.addWidget(self.tableWidget, 0, 0, 1, 1)
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(100, 20, 85, 27))
self.label.setObjectName(_fromUtf8("label"))
self.AddTask = QtGui.QPushButton(self.centralwidget)
self.AddTask.setGeometry(QtCore.QRect(610, 20, 85, 27))
self.AddTask.setObjectName(_fromUtf8("AddTask"))
self.gridLayoutWidget.raise_()
self.label.raise_()
self.AddTask.raise_()
self.tableWidget.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Stockroom Adidas", None))
self.tableWidget.setToolTip(_translate("MainWindow", "<html><head/><body><p>This is where you can add new jobs</p></body></html>", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "SKU", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Size", None))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Status", None))
self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:14pt;\">Lobby</span></p></body></html>", None))
self.AddTask.setText(_translate("MainWindow", "Add Task", None))
app.py
from PyQt4.QtCore import QRegExp
from PyQt4.QtGui import QHeaderView
from PyQt4.QtGui import QTableWidgetItem
from main import Ui_MainWindow, QtCore, QtGui
import sys
from newtask import Ui_NewTask
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
resolution = QtGui.QDesktopWidget().screenGeometry()
# positioning window to center
self.move((resolution.width() / 2) - (self.frameSize().width() / 2),
(resolution.height() / 2) - (self.frameSize().height() / 2))
self.setupUi(self)
# Customizing Columns in Table
rowHeader = self.tableWidget.horizontalHeader()
rowHeader.setResizeMode(0, QHeaderView.ResizeToContents)
rowHeader.setResizeMode(1, QHeaderView.Stretch)
rowHeader.setResizeMode(2, QHeaderView.Stretch)
rowHeader.setResizeMode(3, QHeaderView.ResizeToContents)
# self.tableWidget.resizeColumnsToContents()
# self.tableWidget.resizeRowsToContents()
# When clicked on Add Task button
def showDialog():
newdialog = NewTask(parent=self)
newdialog.show()
if newdialog.exec_():
# adding rows to the table
for i in range(len(newdialog.data)):
currentrow = self.tableWidget.rowCount()
self.tableWidget.insertRow(currentrow)
for j in range(len(newdialog.data[i])):
self.tableWidget.setItem(currentrow, j, newdialog.data[i][j])
self.AddTask.clicked.connect(showDialog)
header = self.tableWidget.verticalHeader()
header.setResizeMode(0, QHeaderView.Stretch)
# header.setDefaultSectionSize(12)
I have another class which inherits QDialog which has lot code, don't want to make this complex, not posting it... you can seee that when the AddTask button is clicked I am connecting the signal to showDialog function. There I am adding rows, I don't know where I am doing wrong, I have tried all the below possible ways.
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
OR
header.setResizeMode(QHeaderView.Stretch)
OR
header.setResizeMode(QHeaderView.Fixed)
OR
self.tableWidget.rowHeight(20)
OR
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
None of them worked... Please help...
EDIT1:
When I try to add rows in qtdesigner, facing the same problem, attaching the image here
QtDesigner
May be self.tableWidget.verticalHeader().setStretchLastSection(True) is causing problem.
Very much a beginner on the UI side of things!
I'm trying to transfer a list from a dialog to the main window (when a button is pressed) and then transfer a new list back to the window when the dialog is accepted.
Here's a MWE of what I'm working on to give you an idea.
It should print out the following,
Desired output:
[b]
.
[b]
[a]
.
[a]
Achieved output:
[b]
.
[b]
[a]
.
[b]
Thanks in advance!
MWE:
import sys
from PyQt4 import QtCore, QtGui#pyqt5, QtWidgets
from Scrabble_gui import Ui_MainWindow
from Input_letters_gui import Ui_Dialog as Popup
class ScrabbleGuiProgram(Ui_MainWindow):
def __init__(self, dialog):
Ui_MainWindow.__init__(self)
self.setupUi(dialog)
self.In_letters.clicked.connect(self.Input_letters)
def Input_letters(self):
"""Prints "b" and opens pop up.
after popup is closed, prints "a"
"""
letter_list = ["b"]
#app = QtGui.QApplication(sys.argv)
print(letter_list)
print(".")
PUDialog = QtGui.QDialog()
pop = Input_letters_popup(PUDialog, letter_list)
PUDialog.exec_()
print(".")
print(letter_list)
class Input_letters_popup(Popup):
def __init__(self, dialog, letter_list):
"""when open prints incoming list ("b")
"""
print(letter_list)
Popup.__init__(self)
self.setupUi(dialog)
self.buttonBox.accepted.connect(self.Get_letters)#lambda: self.Get_letters)
#self.buttonBox.rejected.connect()
def Get_letters(self):
"""on popup exit prints new list ("a")
"""
letters = ["a"]
print(letters)
return letters
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QMainWindow()
prog = ScrabbleGuiProgram(Dialog)#Input_letters_popup(Dialog)
Dialog.show()
sys.exit(app.exec_())
Scrabble_gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Scrabble.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(406, 267)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.Gen_words = QtGui.QPushButton(self.centralwidget)
self.Gen_words.setGeometry(QtCore.QRect(250, 20, 121, 28))
self.Gen_words.setObjectName(_fromUtf8("Gen_words"))
self.Long_word = QtGui.QLineEdit(self.centralwidget)
self.Long_word.setGeometry(QtCore.QRect(229, 100, 141, 22))
self.Long_word.setReadOnly(True)
self.Long_word.setObjectName(_fromUtf8("Long_word"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(279, 180, 41, 21))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(99, 143, 131, 21))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.High_score = QtGui.QLineEdit(self.centralwidget)
self.High_score.setGeometry(QtCore.QRect(319, 180, 51, 22))
self.High_score.setReadOnly(True)
self.High_score.setObjectName(_fromUtf8("High_score"))
self.High_word = QtGui.QLineEdit(self.centralwidget)
self.High_word.setGeometry(QtCore.QRect(229, 143, 141, 22))
self.High_word.setReadOnly(True)
self.High_word.setObjectName(_fromUtf8("High_word"))
self.In_letters = QtGui.QPushButton(self.centralwidget)
self.In_letters.setGeometry(QtCore.QRect(90, 57, 121, 28))
self.In_letters.setObjectName(_fromUtf8("In_letters"))
self.Gen_letters = QtGui.QPushButton(self.centralwidget)
self.Gen_letters.setGeometry(QtCore.QRect(90, 20, 121, 28))
self.Gen_letters.setObjectName(_fromUtf8("Gen_letters"))
self.letters = QtGui.QListWidget(self.centralwidget)
self.letters.setGeometry(QtCore.QRect(20, 50, 41, 161))
self.letters.setObjectName(_fromUtf8("letters"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(145, 100, 81, 20))
self.label.setObjectName(_fromUtf8("label"))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 20, 41, 21))
self.label_4.setObjectName(_fromUtf8("label_4"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 406, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Descrabbler", None))
self.Gen_words.setText(_translate("MainWindow", "Best Words", None))
self.label_2.setText(_translate("MainWindow", "Score", None))
self.label_3.setText(_translate("MainWindow", "Highest Scoring Word", None))
self.In_letters.setText(_translate("MainWindow", "Input Letters", None))
self.Gen_letters.setText(_translate("MainWindow", "Generate Letters", None))
self.label.setText(_translate("MainWindow", "Longest Word", None))
self.label_4.setText(_translate("MainWindow", "Letters", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Input_letters_gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Input_letters.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 173)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 90, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.ltr1 = QtGui.QLineEdit(Dialog)
self.ltr1.setGeometry(QtCore.QRect(20, 40, 31, 22))
self.ltr1.setObjectName(_fromUtf8("ltr1"))
self.ltr2 = QtGui.QLineEdit(Dialog)
self.ltr2.setGeometry(QtCore.QRect(60, 40, 31, 22))
self.ltr2.setObjectName(_fromUtf8("ltr2"))
self.ltr3 = QtGui.QLineEdit(Dialog)
self.ltr3.setGeometry(QtCore.QRect(100, 40, 31, 22))
self.ltr3.setObjectName(_fromUtf8("ltr3"))
self.ltr4 = QtGui.QLineEdit(Dialog)
self.ltr4.setGeometry(QtCore.QRect(140, 40, 31, 22))
self.ltr4.setObjectName(_fromUtf8("ltr4"))
self.ltr5 = QtGui.QLineEdit(Dialog)
self.ltr5.setGeometry(QtCore.QRect(180, 40, 31, 22))
self.ltr5.setObjectName(_fromUtf8("ltr5"))
self.ltr6 = QtGui.QLineEdit(Dialog)
self.ltr6.setGeometry(QtCore.QRect(220, 40, 31, 22))
self.ltr6.setObjectName(_fromUtf8("ltr6"))
self.ltr7 = QtGui.QLineEdit(Dialog)
self.ltr7.setGeometry(QtCore.QRect(260, 40, 31, 22))
self.ltr7.setObjectName(_fromUtf8("ltr7"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Input Letters", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Reading data in from a simple dict d={key:['desc',dateadded]}
Goal is to add data into table view and sort by date in the list. Cannot get any data to add when trying to put table on main window.
The issue lies with how I am placing the table on the main window. I have a working widget with table and am now trying to combine the two files. Currently the application crashes. Dont want you to write the code but a nudge in the right direction would be a big help.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'display.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4.QtGui import (QMainWindow, QApplication)
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def __init__(self):
#app = QApplication(sys.argv)
self.window = QMainWindow()
self.setupUi(self.window)
self.window.show()
self.setmydata()
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(296, 478)
MainWindow.setIconSize(QtCore.QSize(0, 0))
MainWindow.setAnimated(False)
MainWindow.setTabShape(QtGui.QTabWidget.Rounded)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableView = QtGui.QTableView(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(20, 50, 261, 391))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableView.sizePolicy().hasHeightForWidth())
self.tableView.setSizePolicy(sizePolicy)
self.tableView.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers)
self.tableView.setDragEnabled(True)
self.tableView.setAlternatingRowColors(True)
self.tableView.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
self.tableView.setSortingEnabled(False)
self.tableView.setObjectName(_fromUtf8("tableView"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 10, 121, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(160, 10, 121, 31))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 296, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def setmydata(self):
d={"12345678":["do some stuff here that needs to be in a large cell to wrap text","1"],"12343378":["do stuff","1"]}
self.data = {}
keys=d.keys()
self.data['col1']=keys
self.data['col2']=[]
for key in keys:
self.data['col2'].append(d[key][0])
horHeaders = []
for n, key in enumerate(sorted(self.data.keys())):
horHeaders.append(key)
for m, item in enumerate(self.data[key]):
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Add New", None))
self.pushButton_2.setText(_translate("MainWindow", "Save", None))
app = QApplication(sys.argv)
frame = Ui_MainWindow()
#frame.show()
app.exec_()
After digging through the code I found line
self.tableView = QtGui.QTableView(self.centralwidget)
needed to be QTableWidget rather than a View. This allows for me to set values and so on.
My purpose is to make drag and drop between two QTreeViews. Eg: drag on item from a local treeview to a remote treeview, the remote treeview will accept signal and trigger one function (eg: print some thing, so I know it succeeded).
I have searched some posts, they just do with one QTreeView, and I'm still confused how to use drag and drop. This is my full code, including the UI.
main.py
# coding=utf-8
__author__ = 'Administrator'
import os, re, sys, time, math
import ConfigParser
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from MainWindow_ui import Ui_MainWindow
import AccountDialog_ui
######## Config ini File Part#######################
def create_conf_ini(ipaddress='', username='', password='', secret='',mode="a+"):
conf = ConfigParser.ConfigParser()
conf.add_section("logininfor")
conf.set("logininfor", "ipaddress", ipaddress)
conf.set("logininfor", "username", username)
conf.set("logininfor", "password", password)
conf.set("logininfor", "secret", secret)
f = open('config.ini', mode)
conf.write(f)
f.close()
### get conf file
def get_infor_from_config():
conf = ConfigParser.ConfigParser()
if os.path.isfile('config.ini'):
conf.read("config.ini")
ipaddress = conf.get("logininfor", "ipaddress")
username = conf.get("logininfor", "username")
password = conf.get("logininfor", "password")
secret = conf.get("logininfor", "secret")
return ipaddress, username, password, secret
else:
create_conf_ini()
return '', '', '', ''
###############################
class S3MiniToolsView(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.actionAccount_Setting.triggered.connect(self.accountsetting_dlg)
self.init_localtreeview()
self.init_remotetreeview()
QtGui.QTreeView.connect(self.remote_Treeview, QtCore.SIGNAL('dropEvent()'), self.additem)
(ipaddress, username, password, secret) = get_infor_from_config()
self.servername_mainw.setText(ipaddress)
self.uid_mainw.setText(username)
self.accesskey_mainw.setText(password)
self.secretkey_mainw.setText(secret)
def init_localtreeview(self):
self.fileSystemModel = QFileSystemModel(self.local_Treeview)
self.fileSystemModel.setReadOnly(False)
#self.fileSystemModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot | QDir.Drives)
root = self.fileSystemModel.setRootPath("")
self.local_Treeview.setModel(self.fileSystemModel)
self.local_Treeview.setRootIndex(root)
self.local_Treeview.setDragDropMode(QtGui.QAbstractItemView.DragOnly)
#QtCore.QObject.connect(self.local_Treeview.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.test)
#self.local_Treeview.clicked[QtCore.QModelIndex].connect(self.test)
self.local_Treeview.clicked.connect(self.test)
self.local_Treeview.setDragEnabled(True)
def init_remotetreeview(self):
self.remote_Treeview.setDragDropMode(QtGui.QAbstractItemView.DropOnly)
# self.fileSystemModel = QFileSystemModel(self.local_Treeview)
# self.fileSystemModel.setReadOnly(False)
# self.remote_Treeview.setModel(self.fileSystemModel)
self.model = QtGui.QStandardItemModel(self.remote_Treeview)
self.remote_Treeview.setModel(self.model)
self.remote_Treeview.setAcceptDrops(True)
def dragEnterEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()
def dropEvent(self, e):
print 'In dropEvent'
# item = QtGui.QStandardItem("did ok")
# self.model.appendRow(item)
#self.remote_Treeview.(e.mimeData().text())
def additem(self):
item = QtGui.QStandardItem("did ok")
self.model.appendRow(item)
##QtCore.pyqtSlot("QItemSelection, QItemSelection")
#QtCore.pyqtSlot(QtCore.QModelIndex)
def test(self, index):
indexItem = self.fileSystemModel.index(index.row(), 0, index.parent())
# path or filename selected
fileName = self.fileSystemModel.fileName(indexItem)
# full path/filename selected
filePath = self.fileSystemModel.filePath(indexItem)
print("hello!")
print(fileName)
print(filePath)
def accountsetting_dlg(self):
print "AccountSetting"
accountdlg = QuickConnectDlg(self)
accountdlg.lineEdit_servername.setText( 'ceph-radosgw.lab.com')
accountdlg.lineEdit_accountname.setText( 'johndoe')
accountdlg.lineEdit_accesskey.setText( 'CIXN1L1B42JAYGV6KSIT')
accountdlg.lineEdit_secretkey.setText('17YTAqVBL60StWQniDNWoAH04bScFbjxAxpxNFCg')
accountdlg.lineEdit_httpport.setText( '8080')
accountdlg.lineEdit_httpsport.setText( '443')
if accountdlg.exec_():
servername = accountdlg.lineEdit_servername.text()
accountname = accountdlg.lineEdit_accountname.text()
accesskey = accountdlg.lineEdit_accesskey.text()
secretkey = accountdlg.lineEdit_secretkey.text()
create_conf_ini(servername, accountname, accesskey, secretkey, 'r+')
return
class QuickConnectDlg(QDialog, AccountDialog_ui.Ui_accountDialog):
def __init__(self, parent=None):
super(QuickConnectDlg, self).__init__(parent)
self.setupUi(self)
def main():
app = QApplication(sys.argv)
window = S3MiniToolsView()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
this is the MainWindow_ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created: Thu Aug 28 09:43:28 2014
# by: PyQt4 UI code generator 4.11.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(961, 623)
MainWindow.setInputMethodHints(QtCore.Qt.ImhNone)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setEnabled(True)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayoutWidget_3 = QtGui.QWidget(self.centralwidget)
self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(20, 550, 91, 41))
self.verticalLayoutWidget_3.setObjectName(_fromUtf8("verticalLayoutWidget_3"))
self.verticalLayout_3 = QtGui.QVBoxLayout(self.verticalLayoutWidget_3)
self.verticalLayout_3.setMargin(0)
self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
self.pButton_Exportxls = QtGui.QPushButton(self.verticalLayoutWidget_3)
self.pButton_Exportxls.setObjectName(_fromUtf8("pButton_Exportxls"))
self.verticalLayout_3.addWidget(self.pButton_Exportxls)
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(0, 130, 951, 20))
self.label_4.setFrameShape(QtGui.QFrame.Box)
self.label_4.setFrameShadow(QtGui.QFrame.Sunken)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.horizontalGroupBox = QtGui.QGroupBox(self.centralwidget)
self.horizontalGroupBox.setGeometry(QtCore.QRect(0, 20, 961, 51))
self.horizontalGroupBox.setObjectName(_fromUtf8("horizontalGroupBox"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalGroupBox)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.label_10 = QtGui.QLabel(self.horizontalGroupBox)
self.label_10.setFrameShape(QtGui.QFrame.NoFrame)
self.label_10.setFrameShadow(QtGui.QFrame.Sunken)
self.label_10.setAlignment(QtCore.Qt.AlignCenter)
self.label_10.setObjectName(_fromUtf8("label_10"))
self.horizontalLayout.addWidget(self.label_10)
self.servername_mainw = QtGui.QLineEdit(self.horizontalGroupBox)
self.servername_mainw.setObjectName(_fromUtf8("servername_mainw"))
self.horizontalLayout.addWidget(self.servername_mainw)
self.label_5 = QtGui.QLabel(self.horizontalGroupBox)
self.label_5.setFrameShape(QtGui.QFrame.NoFrame)
self.label_5.setFrameShadow(QtGui.QFrame.Sunken)
self.label_5.setAlignment(QtCore.Qt.AlignCenter)
self.label_5.setObjectName(_fromUtf8("label_5"))
self.horizontalLayout.addWidget(self.label_5)
self.uid_mainw = QtGui.QLineEdit(self.horizontalGroupBox)
self.uid_mainw.setObjectName(_fromUtf8("uid_mainw"))
self.horizontalLayout.addWidget(self.uid_mainw)
self.label_3 = QtGui.QLabel(self.horizontalGroupBox)
self.label_3.setFrameShape(QtGui.QFrame.NoFrame)
self.label_3.setFrameShadow(QtGui.QFrame.Sunken)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.horizontalLayout.addWidget(self.label_3)
self.accesskey_mainw = QtGui.QLineEdit(self.horizontalGroupBox)
self.accesskey_mainw.setObjectName(_fromUtf8("accesskey_mainw"))
self.horizontalLayout.addWidget(self.accesskey_mainw)
self.label_6 = QtGui.QLabel(self.horizontalGroupBox)
self.label_6.setFrameShape(QtGui.QFrame.NoFrame)
self.label_6.setFrameShadow(QtGui.QFrame.Sunken)
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName(_fromUtf8("label_6"))
self.horizontalLayout.addWidget(self.label_6)
self.secretkey_mainw = QtGui.QLineEdit(self.horizontalGroupBox)
self.secretkey_mainw.setInputMethodHints(QtCore.Qt.ImhHiddenText|QtCore.Qt.ImhNoAutoUppercase|QtCore.Qt.ImhNoPredictiveText|QtCore.Qt.ImhPreferNumbers)
self.secretkey_mainw.setText(_fromUtf8(""))
self.secretkey_mainw.setEchoMode(QtGui.QLineEdit.Password)
self.secretkey_mainw.setObjectName(_fromUtf8("secretkey_mainw"))
self.horizontalLayout.addWidget(self.secretkey_mainw)
spacerItem = QtGui.QSpacerItem(20, 10, QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.connect_pButton = QtGui.QPushButton(self.horizontalGroupBox)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.connect_pButton.sizePolicy().hasHeightForWidth())
self.connect_pButton.setSizePolicy(sizePolicy)
self.connect_pButton.setMinimumSize(QtCore.QSize(75, 23))
self.connect_pButton.setMaximumSize(QtCore.QSize(75, 23))
self.connect_pButton.setObjectName(_fromUtf8("connect_pButton"))
self.horizontalLayout.addWidget(self.connect_pButton)
self.filename = QtGui.QLineEdit(self.centralwidget)
self.filename.setGeometry(QtCore.QRect(190, 540, 110, 20))
self.filename.setObjectName(_fromUtf8("filename"))
self.label_7 = QtGui.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(460, 75, 78, 16))
self.label_7.setFrameShape(QtGui.QFrame.NoFrame)
self.label_7.setFrameShadow(QtGui.QFrame.Sunken)
self.label_7.setAlignment(QtCore.Qt.AlignCenter)
self.label_7.setObjectName(_fromUtf8("label_7"))
self.buckets_cBox_mainw = QtGui.QComboBox(self.centralwidget)
self.buckets_cBox_mainw.setGeometry(QtCore.QRect(103, 100, 111, 20))
self.buckets_cBox_mainw.setObjectName(_fromUtf8("buckets_cBox_mainw"))
self.label_8 = QtGui.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(20, 100, 78, 16))
self.label_8.setFrameShape(QtGui.QFrame.NoFrame)
self.label_8.setFrameShadow(QtGui.QFrame.Sunken)
self.label_8.setAlignment(QtCore.Qt.AlignCenter)
self.label_8.setObjectName(_fromUtf8("label_8"))
self.uri_mainw = QtGui.QLineEdit(self.centralwidget)
self.uri_mainw.setGeometry(QtCore.QRect(103, 76, 261, 20))
self.uri_mainw.setObjectName(_fromUtf8("uri_mainw"))
self.label_9 = QtGui.QLabel(self.centralwidget)
self.label_9.setGeometry(QtCore.QRect(20, 80, 78, 16))
self.label_9.setFrameShape(QtGui.QFrame.NoFrame)
self.label_9.setFrameShadow(QtGui.QFrame.Sunken)
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName(_fromUtf8("label_9"))
self.local_Treeview = QtGui.QTreeView(self.centralwidget)
self.local_Treeview.setGeometry(QtCore.QRect(0, 160, 501, 361))
self.local_Treeview.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked|QtGui.QAbstractItemView.EditKeyPressed|QtGui.QAbstractItemView.SelectedClicked)
self.local_Treeview.setDragEnabled(True)
self.local_Treeview.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.local_Treeview.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
self.local_Treeview.setObjectName(_fromUtf8("local_Treeview"))
self.remote_Treeview = QtGui.QTreeView(self.centralwidget)
self.remote_Treeview.setGeometry(QtCore.QRect(507, 160, 451, 361))
self.remote_Treeview.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked|QtGui.QAbstractItemView.EditKeyPressed|QtGui.QAbstractItemView.SelectedClicked)
self.remote_Treeview.setDragDropMode(QtGui.QAbstractItemView.DropOnly)
self.remote_Treeview.setDefaultDropAction(QtCore.Qt.CopyAction)
self.remote_Treeview.setObjectName(_fromUtf8("remote_Treeview"))
self.root_cBox_mainw = QtGui.QComboBox(self.centralwidget)
self.root_cBox_mainw.setGeometry(QtCore.QRect(536, 70, 141, 20))
self.root_cBox_mainw.setObjectName(_fromUtf8("root_cBox_mainw"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 961, 17))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuW = QtGui.QMenu(self.menubar)
self.menuW.setObjectName(_fromUtf8("menuW"))
self.menu = QtGui.QMenu(self.menubar)
self.menu.setObjectName(_fromUtf8("menu"))
self.menuTools = QtGui.QMenu(self.menubar)
self.menuTools.setObjectName(_fromUtf8("menuTools"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionOpenfile = QtGui.QAction(MainWindow)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("../Gui-test/Resource/OpenFile.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actionOpenfile.setIcon(icon)
self.actionOpenfile.setObjectName(_fromUtf8("actionOpenfile"))
self.actionAccount_Setting = QtGui.QAction(MainWindow)
self.actionAccount_Setting.setObjectName(_fromUtf8("actionAccount_Setting"))
self.menuW.addAction(self.actionOpenfile)
self.menuTools.addAction(self.actionAccount_Setting)
self.menubar.addAction(self.menuW.menuAction())
self.menubar.addAction(self.menuTools.menuAction())
self.menubar.addAction(self.menu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.uid_mainw, self.accesskey_mainw)
MainWindow.setTabOrder(self.accesskey_mainw, self.secretkey_mainw)
MainWindow.setTabOrder(self.secretkey_mainw, self.connect_pButton)
MainWindow.setTabOrder(self.connect_pButton, self.pButton_Exportxls)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "mini S3 File Manager", None))
self.pButton_Exportxls.setText(_translate("MainWindow", "导出到xls", None))
self.label_4.setText(_translate("MainWindow", "Detail Information:", None))
self.horizontalGroupBox.setTitle(_translate("MainWindow", "S3 Storage Account Information:", None))
self.label_10.setText(_translate("MainWindow", "Server:", None))
self.servername_mainw.setToolTip(_translate("MainWindow", "需要检查的设备IP", None))
self.label_5.setText(_translate("MainWindow", "Account name:", None))
self.uid_mainw.setToolTip(_translate("MainWindow", "需要检查的设备IP", None))
self.label_3.setText(_translate("MainWindow", "Access key:", None))
self.label_6.setText(_translate("MainWindow", "Secret key:", None))
self.connect_pButton.setToolTip(_translate("MainWindow", "开始检查", None))
self.connect_pButton.setText(_translate("MainWindow", "Connect", None))
self.label_7.setText(_translate("MainWindow", "Root:", None))
self.label_8.setText(_translate("MainWindow", "My Buckets:", None))
self.label_9.setText(_translate("MainWindow", "URI:", None))
self.menuW.setTitle(_translate("MainWindow", "文件", None))
self.menu.setTitle(_translate("MainWindow", "帮助", None))
self.menuTools.setTitle(_translate("MainWindow", "Tools", None))
self.actionOpenfile.setText(_translate("MainWindow", "Openfile", None))
self.actionAccount_Setting.setText(_translate("MainWindow", "Account Setting", None))
AccountDialog_ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'AccountDialog.ui'
#
# Created: Mon Aug 25 23:41:07 2014
# by: PyQt4 UI code generator 4.11.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_accountDialog(object):
def setupUi(self, accountDialog):
accountDialog.setObjectName(_fromUtf8("accountDialog"))
accountDialog.resize(400, 248)
self.layoutWidget = QtGui.QWidget(accountDialog)
self.layoutWidget.setGeometry(QtCore.QRect(10, 10, 380, 225))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.gridLayout_3 = QtGui.QGridLayout(self.layoutWidget)
self.gridLayout_3.setMargin(0)
self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3"))
self.tabWidget_2 = QtGui.QTabWidget(self.layoutWidget)
self.tabWidget_2.setObjectName(_fromUtf8("tabWidget_2"))
self.tabWidgetPage1_2 = QtGui.QWidget()
self.tabWidgetPage1_2.setObjectName(_fromUtf8("tabWidgetPage1_2"))
self.gridLayout_4 = QtGui.QGridLayout(self.tabWidgetPage1_2)
self.gridLayout_4.setObjectName(_fromUtf8("gridLayout_4"))
self.label_7 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_7.setObjectName(_fromUtf8("label_7"))
self.gridLayout_4.addWidget(self.label_7, 0, 0, 1, 1)
self.lineEdit_servername = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_servername.setObjectName(_fromUtf8("lineEdit_servername"))
self.gridLayout_4.addWidget(self.lineEdit_servername, 0, 1, 1, 2)
self.label_8 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_8.setObjectName(_fromUtf8("label_8"))
self.gridLayout_4.addWidget(self.label_8, 1, 0, 1, 1)
self.lineEdit_accountname = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_accountname.setObjectName(_fromUtf8("lineEdit_accountname"))
self.gridLayout_4.addWidget(self.lineEdit_accountname, 1, 1, 1, 2)
self.label_9 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_9.setObjectName(_fromUtf8("label_9"))
self.gridLayout_4.addWidget(self.label_9, 2, 0, 1, 1)
self.lineEdit_accesskey = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_accesskey.setObjectName(_fromUtf8("lineEdit_accesskey"))
self.gridLayout_4.addWidget(self.lineEdit_accesskey, 2, 1, 1, 2)
self.label_10 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_10.setObjectName(_fromUtf8("label_10"))
self.gridLayout_4.addWidget(self.label_10, 3, 0, 1, 1)
self.lineEdit_secretkey = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_secretkey.setObjectName(_fromUtf8("lineEdit_secretkey"))
self.gridLayout_4.addWidget(self.lineEdit_secretkey, 3, 1, 1, 2)
self.label_11 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_11.setObjectName(_fromUtf8("label_11"))
self.gridLayout_4.addWidget(self.label_11, 4, 0, 1, 1)
self.lineEdit_httpport = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_httpport.setObjectName(_fromUtf8("lineEdit_httpport"))
self.gridLayout_4.addWidget(self.lineEdit_httpport, 4, 1, 1, 1)
self.label_12 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_12.setObjectName(_fromUtf8("label_12"))
self.gridLayout_4.addWidget(self.label_12, 5, 0, 1, 1)
self.lineEdit_httpsport = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_httpsport.setObjectName(_fromUtf8("lineEdit_httpsport"))
self.gridLayout_4.addWidget(self.lineEdit_httpsport, 5, 1, 1, 1)
self.checkBox_https = QtGui.QCheckBox(self.tabWidgetPage1_2)
self.checkBox_https.setObjectName(_fromUtf8("checkBox_https"))
self.gridLayout_4.addWidget(self.checkBox_https, 5, 2, 1, 1)
self.tabWidget_2.addTab(self.tabWidgetPage1_2, _fromUtf8(""))
self.gridLayout_3.addWidget(self.tabWidget_2, 0, 0, 1, 3)
spacerItem = QtGui.QSpacerItem(218, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout_3.addItem(spacerItem, 1, 0, 1, 1)
self.buttonBox = QtGui.QDialogButtonBox(self.layoutWidget)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.gridLayout_3.addWidget(self.buttonBox, 1, 1, 1, 1)
self.retranslateUi(accountDialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), accountDialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), accountDialog.reject)
QtCore.QMetaObject.connectSlotsByName(accountDialog)
def retranslateUi(self, accountDialog):
accountDialog.setWindowTitle(_translate("accountDialog", "Account", None))
self.label_7.setText(_translate("accountDialog", "Server name", None))
self.label_8.setText(_translate("accountDialog", "Account name", None))
self.label_9.setText(_translate("accountDialog", "Access key", None))
self.label_10.setText(_translate("accountDialog", "Secret key", None))
self.label_11.setText(_translate("accountDialog", "HTTP Port", None))
self.lineEdit_httpport.setText(_translate("accountDialog", "8080", None))
self.label_12.setText(_translate("accountDialog", "HTTPS Port", None))
self.lineEdit_httpsport.setText(_translate("accountDialog", "443", None))
self.checkBox_https.setText(_translate("accountDialog", "Connect usring SSL/HTTPS", None))
self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.tabWidgetPage1_2), _translate("accountDialog", "Account", None))
If your exchange data between 2 widget, I suggest to use QTreeWidget more than QTreeView because data in QTreeWidget can edit dynamic data, row and value. In drag and drop between 2 QTreeWidget we can equivalent them "copy and delete".
Easy to implement between 2 QTreeWidget, your just handle when drag enter event in QTreeWidget and copy to new QTreeWidget. (If your internal move, I will delete old data by itself) Next, Create your own signal to handle them if have move data between QTreeWidget. Last, integrate it in your class or widget.
Example (Not your code, But your can implement it by yourself), Like this;
import sys
from PyQt4 import QtCore, QtGui
class QCustomTreeWidget (QtGui.QTreeWidget):
itemMoveOutActivated = QtCore.pyqtSignal(object)
itemNewMoveActivated = QtCore.pyqtSignal(object)
def __init__ (self, parent = None):
super(QCustomTreeWidget, self).__init__(parent)
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
def dragEnterEvent (self, eventQDragEnterEvent):
sourceQCustomTreeWidget = eventQDragEnterEvent.source()
if isinstance(sourceQCustomTreeWidget, QCustomTreeWidget) and (self is not sourceQCustomTreeWidget):
eventQDragEnterEvent.accept()
else:
QtGui.QTreeWidget.dragEnterEvent(self, eventQDragEnterEvent)
def dropEvent (self, eventQDropEvent):
sourceQCustomTreeWidget = eventQDropEvent.source()
if isinstance(sourceQCustomTreeWidget, QCustomTreeWidget) and (self is not sourceQCustomTreeWidget):
sourceQTreeWidgetItem = sourceQCustomTreeWidget.currentItem()
destinationQTreeWidgetItem = sourceQTreeWidgetItem.clone()
self.addTopLevelItem(destinationQTreeWidgetItem)
sourceQCustomTreeWidget.itemMoveOutActivated.emit(destinationQTreeWidgetItem)
self.itemNewMoveActivated.emit(destinationQTreeWidgetItem)
else:
QtGui.QTreeWidget.dropEvent(self, eventQDropEvent)
class QCustomQWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
self.my1QCustomTreeWidget = QCustomTreeWidget(self)
self.my2QCustomTreeWidget = QCustomTreeWidget(self)
self.my1QCustomTreeWidget.itemMoveOutActivated.connect(self.itemMoveOutActivatedCallBack1)
self.my2QCustomTreeWidget.itemMoveOutActivated.connect(self.itemMoveOutActivatedCallBack2)
self.my1QCustomTreeWidget.itemNewMoveActivated.connect(self.itemNewMoveActivatedCallBack1)
self.my2QCustomTreeWidget.itemNewMoveActivated.connect(self.itemNewMoveActivatedCallBack2)
listsExampleQTreeWidgetItem = [QtGui.QTreeWidgetItem([name]) for name in ['Part A', 'Part B', 'Part C']]
self.my1QCustomTreeWidget.addTopLevelItems(listsExampleQTreeWidgetItem)
self.allQHBoxLayout = QtGui.QHBoxLayout()
self.allQHBoxLayout.addWidget(self.my1QCustomTreeWidget)
self.allQHBoxLayout.addWidget(self.my2QCustomTreeWidget)
self.setLayout(self.allQHBoxLayout)
#QtCore.pyqtSlot(QtGui.QTreeWidgetItem)
def itemMoveOutActivatedCallBack1 (self, goneQTreeWidgetItem):
print 'QTreeWidget 1 has move QTreeWidgetItem to Another QTreeWidget'
#QtCore.pyqtSlot(QtGui.QTreeWidgetItem)
def itemMoveOutActivatedCallBack2 (self, goneQTreeWidgetItem):
print 'QTreeWidget 2 has move QTreeWidgetItem to Another QTreeWidget'
#QtCore.pyqtSlot(QtGui.QTreeWidgetItem)
def itemNewMoveActivatedCallBack1 (self, newQTreeWidgetItem):
print 'Another QTreeWidget has move QTreeWidgetItem in QTreeWidget 1'
#QtCore.pyqtSlot(QtGui.QTreeWidgetItem)
def itemNewMoveActivatedCallBack2 (self, newQTreeWidgetItem):
print 'Another QTreeWidget has move QTreeWidgetItem in QTreeWidget 2'
app = QtGui.QApplication(sys.argv)
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.show()
sys.exit(app.exec_())
Hope is helps,