PyQt: Force popup window to be at the front - python

I have a simple popup about window in my application. It has a button "Ok" which closes the about window. I want to make that window 'exitable' only via the "Ok" button so you wouldn't be able to un-focus and get out of it.
That would be the same behavior as Internet Explorer about window.
Thanks a lot!
EDIT: The code!
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from os import system, chdir,getcwd
from time import sleep
from subprocess import Popen
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 MyPopup(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("./about.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.setWindowIcon(icon)
resolution = QtGui.QDesktopWidget().screenGeometry()
self.move((resolution.width() / 2) - (self.frameSize().width() / 2),(resolution.height() / 2) - (self.frameSize().height() / 2))
pic = QtGui.QLabel(self)
pic.setGeometry(20, 20, 128, 128)
pic.setPixmap(QtGui.QPixmap(getcwd() + "/icon.png"))
self.setMinimumSize(QtCore.QSize(420, 300))
self.setMaximumSize(QtCore.QSize(420, 300))
labele = QtGui.QLabel(self)
labele.setGeometry(QtCore.QRect(158, 20 , 2000, 50))
font = QtGui.QFont()
font.setPointSize(26)
labele.setText(_translate("MyPopup", "Auto Shutdown", None))
labele.setFont(font)
labelel = QtGui.QLabel(self)
labelel.setGeometry(QtCore.QRect(158, 70 , 2000, 25))
font = QtGui.QFont()
font.setPointSize(16)
labelel.setText(_translate("MyPopup", "Jan Murić", None))
labelel.setFont(font)
labelo = QtGui.QLabel(self)
labelo.setGeometry(QtCore.QRect(100, 175 , 300, 100))
font = QtGui.QFont()
font.setPointSize(10)
labelo.setText(_translate("MyPopup", "FREEWARE\n\nVersion: 1.0\nE-Mail: janek.muric#gmail.com", None))
labelo.setFont(font)
exitButton = QtGui.QPushButton(self)
exitButton.setGeometry(QtCore.QRect(347, 276, 71, 23))
exitButton.setText(_translate("MyPopup", "Ok", None))
exitButton.clicked.connect(self.quit)
def quit(self):
self.close()
class Ui_MainWindow(object):
global x
x = 0
def setupUi(self, MainWindow, parent=None):
super(Ui_MainWindow, self)
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(261, 390)
MainWindow.setMinimumSize(QtCore.QSize(261, 390))
MainWindow.setMaximumSize(QtCore.QSize(261, 390))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("./icon.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
MainWindow.setUnifiedTitleAndToolBarOnMac(False)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(261, 390))
self.centralwidget.setMaximumSize(QtCore.QSize(261, 390))
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.action = QtGui.QComboBox(self.centralwidget)
self.action.setGeometry(QtCore.QRect(104, 60, 91, 22))
self.action.setObjectName(_fromUtf8("action"))
self.action.addItem(_fromUtf8(""))
self.action.addItem(_fromUtf8(""))
self.action.addItem(_fromUtf8(""))
self.action.addItem(_fromUtf8(""))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 10, 221, 21))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(50, 60, 51, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.groupBox = QtGui.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(10, 100, 241, 191))
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.selectTime = QtGui.QRadioButton(self.groupBox)
self.selectTime.setGeometry(QtCore.QRect(20, 30, 82, 17))
self.selectTime.setObjectName(_fromUtf8("selectTime"))
self.buttonGroup_2 = QtGui.QButtonGroup(MainWindow)
self.buttonGroup_2.setObjectName(_fromUtf8("buttonGroup_2"))
self.buttonGroup_2.addButton(self.selectTime)
self.selectDate = QtGui.QRadioButton(self.groupBox)
self.selectDate.setGeometry(QtCore.QRect(20, 110, 82, 17))
self.selectDate.setObjectName(_fromUtf8("selectDate"))
self.buttonGroup_2.addButton(self.selectDate)
self.label_3 = QtGui.QLabel(self.groupBox)
self.label_3.setGeometry(QtCore.QRect(116, 60, 61, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.label_3.setFont(font)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.relativeTime = QtGui.QTimeEdit(self.groupBox)
self.relativeTime.setGeometry(QtCore.QRect(40, 60, 61, 22))
self.relativeTime.setObjectName(_fromUtf8("relativeTime"))
self.label_4 = QtGui.QLabel(self.groupBox)
self.label_4.setGeometry(QtCore.QRect(40, 140, 21, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.label_4.setFont(font)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.dateTime = QtGui.QDateTimeEdit(self.groupBox)
self.dateTime.setGeometry(QtCore.QRect(70, 140, 111, 22))
self.dateTime.setObjectName(_fromUtf8("dateTime"))
self.forceCheck = QtGui.QCheckBox(self.centralwidget)
self.forceCheck.setGeometry(QtCore.QRect(10, 300, 81, 21))
self.forceCheck.setObjectName(_fromUtf8("forceCheck"))
self.forceCheck.stateChanged.connect(self.changeText)
self.startButton = QtGui.QPushButton(self.centralwidget)
self.startButton.setGeometry(QtCore.QRect(170, 300, 71, 23))
self.startButton.setObjectName(_fromUtf8("startButton"))
self.startButton.clicked.connect(self.start)
self.label_5 = QtGui.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(0, 330, 251, 20))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.label_5.setAlignment(QtCore.Qt.AlignCenter)
self.label_6 = QtGui.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(0, 360, 261, 20))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.dateTime.setDateTime(QtCore.QDateTime.currentDateTime())
self.about = QtGui.QPushButton(self.centralwidget)
self.about.setGeometry(QtCore.QRect(240, 0, 21, 21))
self.about.setObjectName(_fromUtf8("about"))
self.about.clicked.connect(self.aboutWindow)
global tim
global act
global forc
tim = QtCore.QTime.currentTime()
tim = str(tim.hour()) + ":" + str(tim.minute())
act = "Shut Down"
forc = ""
self.label_3.setEnabled(False)
self.relativeTime.setEnabled(False)
self.label_4.setEnabled(False)
self.dateTime.setEnabled(False)
self.selectTime.toggled.connect(self.greyOut2)
self.selectDate.toggled.connect(self.greyOut1)
self.action.currentIndexChanged.connect(self.changeText)
self.dateTime.dateTimeChanged.connect(self.changeText)
self.relativeTime.timeChanged.connect(self.changeText)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Auto Shutdown", None))
self.action.setItemText(0, _translate("MainWindow", "Shut Down", None))
self.action.setItemText(1, _translate("MainWindow", "Restart", None))
self.action.setItemText(2, _translate("MainWindow", "Go to boot menu", None))
self.action.setItemText(3, _translate("MainWindow", "Hibernate", None))
self.label.setText(_translate("MainWindow", "Please chose an action and then a time for it.", None))
self.label_2.setText(_translate("MainWindow", "Action:", None))
self.groupBox.setTitle(_translate("MainWindow", "Time Schedule", None))
self.selectTime.setText(_translate("MainWindow", "Relative Time", None))
self.selectDate.setText(_translate("MainWindow", "Exact Time", None))
self.label_3.setText(_translate("MainWindow", "From now", None))
self.label_4.setText(_translate("MainWindow", "On", None))
self.forceCheck.setText(_translate("MainWindow", "Force Action", None))
self.startButton.setText(_translate("MainWindow", "Start", None))
self.about.setText(_translate("MainWindow", "?", None))
self.label_5.setText(_translate("MainWindow", "Your computer will " + act + " in ", None))
self.label_6.setText(_translate("MainWindow", tim + forc, None))
self.startButton.setEnabled(False)
def greyOut1(self): #selectDate on
self.label_3.setEnabled(False)
self.relativeTime.setEnabled(False)
self.label_4.setEnabled(True)
self.dateTime.setEnabled(True)
self.startButton.setEnabled(True)
def greyOut2(self): #selectTime on
self.label_3.setEnabled(True)
self.relativeTime.setEnabled(True)
self.label_4.setEnabled(False)
self.dateTime.setEnabled(False)
self.startButton.setEnabled(True)
def changeText(self):
if self.selectDate.isChecked():
global forc
if self.forceCheck.isChecked():
forc = " (Force)"
else:
forc = ""
act = self.action.currentText()
tim = self.dateTime.dateTime()
timm = tim.time()
dat = tim.date()
timee = str(dat.day()) + "." + str(dat.month()) + "." + str(dat.year()) + " " + str(timm.hour()) + ":" + str(timm.minute())
self.label_5.setText(_translate("MainWindow", "Your computer will " + act + " at ", None))
self.label_6.setText(_translate("MainWindow", timee + " " + forc,None))
else:
if self.forceCheck.isChecked():
forc = "(Force)"
else:
forc = ""
act = self.action.currentText()
tim = self.relativeTime.time()
now = QtCore.QTime.currentTime()
hr = now.hour() + tim.hour()
mn = now.minute() + tim.minute()
timee = str(hr) + ":" + str(mn)
self.label_5.setText(_translate("MainWindow", "Your computer will " + act + " at ", None))
self.label_6.setText(_translate("MainWindow", timee + forc,None))
def start(self):
global x
if x == 0:
x = 1
palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Foreground,QtCore.Qt.darkGreen)
self.label_5.setPalette(palette)
self.label_6.setPalette(palette)
self.startButton.setText("Stop")
self.label_3.setEnabled(False)
self.relativeTime.setEnabled(False)
self.label_4.setEnabled(False)
self.dateTime.setEnabled(False)
self.action.setEnabled(False)
self.selectTime.setEnabled(False)
self.selectDate.setEnabled(False)
self.forceCheck.setEnabled(False)
global f
global t
global s
if self.forceCheck.isChecked():
f = " /f"
else:
f = ""
if self.action.currentText() == "Shut Down":
s = "/s"
elif self.action.currentText() == "Restart":
s = "/r"
elif self.action.currentText() == "Go to boot menu":
s = "/o"
elif self.action.currentText() == "Hibernate":
s = "/h"
if self.selectTime.isChecked():
tim = self.relativeTime.time()
now = QtCore.QTime.currentTime()
timm = int(tim.hour())
tinn = int(tim.minute())
hsec = timm * 60
hsec = hsec * 60
msec = tinn * 60
t = msec + hsec
chdir(".")
Popen("shutdwn " + s + " /t " + str(t) + f)
if self.selectDate.isChecked():
tim = self.dateTime.dateTime()
now = QtCore.QDateTime.currentDateTime()
t = tim.toTime_t() - now.toTime_t()
Popen("shutdwn " + s + " /t " + str(t) + f)
else:
x = 0
palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Foreground,QtCore.Qt.black)
self.label_5.setPalette(palette)
self.label_6.setPalette(palette)
chdir(".")
Popen("shutdwn /a")
self.action.setEnabled(True)
self.selectTime.setEnabled(True)
self.selectDate.setEnabled(True)
self.forceCheck.setEnabled(True)
self.startButton.setText("Start")
#QtCore.pyqtSlot()
def aboutWindow(self):
self.w = MyPopup()
self.w.show()
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_())

Make your Mypopup class extend QDialog then call _exec function it will make your popup modal so you wont be able to interact with rest of application without dealing popup first
class MyPopup(QDialog):
Qdialog.__init__.(self)
//and on your aboutWindow function call _exec function
self.w.exec_()

Instead of inheriting from QWidget, use a QDialog with setModal(True).
class MyPopup(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setModal(True)
icon = QtGui.QIcon()
Hope it helps!

You can make your popup modal by having it inherit from QDialog rather than QWidget then show it with self.w.exec_()
Not sure if you want the dialog close button (X) to be disabled or hidden completely, but you can see if self.setWindowFlags(Qt.FramelessWindowHint) does what you want.

Related

Pyqt5 Stop Function with Stop Button

We control our process with variables like "self.runHunt". If we get the "True" value, we start the time and print our text. When we press stop, we tell it to take the value "false" and stop the process. I think there is no problem here.
But;
Time starts when our function is "while true". At that time, even if we press the stop button, time continues and our value still remains as "True" and cannot update itself. However, after the time expires, the value becomes "false".
My question is how can I get the "false" variable without waiting for the time to complete?
from PyQt5 import QtCore, QtGui, QtWidgets
from operator import index
from PyQt5 import QtCore, QtGui, QtWidgets, QtTest
import random
import threading
import sys
myKeyboard = Controller()
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(850, 350)
Dialog.setMinimumSize(QtCore.QSize(850, 350))
Dialog.setMaximumSize(QtCore.QSize(850, 350))
self.stopPushButton = QtWidgets.QPushButton(Dialog)
self.stopPushButton.setGeometry(QtCore.QRect(580, 90, 241, 51))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.stopPushButton.setFont(font)
self.stopPushButton.setIconSize(QtCore.QSize(32, 32))
self.stopPushButton.setObjectName("stopPushButton")
self.startPushButton = QtWidgets.QPushButton(Dialog)
self.startPushButton.setGeometry(QtCore.QRect(580, 20, 241, 51))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.startPushButton.setFont(font)
self.startPushButton.setIconSize(QtCore.QSize(32, 32))
self.startPushButton.setObjectName("startPushButton")
self.loopGroup = QtWidgets.QGroupBox(Dialog)
self.loopGroup.setEnabled(True)
self.loopGroup.setGeometry(QtCore.QRect(10, 10, 541, 221))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.loopGroup.setFont(font)
self.loopGroup.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.loopGroup.setAcceptDrops(False)
self.loopGroup.setAutoFillBackground(False)
self.loopGroup.setFlat(False)
self.loopGroup.setCheckable(False)
self.loopGroup.setObjectName("loopGroup")
self.huntCheckBox = QtWidgets.QCheckBox(self.loopGroup)
self.huntCheckBox.setGeometry(QtCore.QRect(240, 35, 35, 20))
self.huntCheckBox.setText("")
self.huntCheckBox.setObjectName("huntCheckBox")
self.huntLabel = QtWidgets.QLabel(self.loopGroup)
self.huntLabel.setGeometry(QtCore.QRect(20, 35, 61, 21))
font = QtGui.QFont()
font.setFamily("Microsoft JhengHei UI")
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.huntLabel.setFont(font)
self.huntLabel.setObjectName("huntLabel")
self.HuntinfoBrowser = QtWidgets.QTextBrowser(self.loopGroup)
self.HuntinfoBrowser.setGeometry(QtCore.QRect(280, 25, 251, 31))
self.HuntinfoBrowser.setObjectName("HuntinfoBrowser")
self.huntCmbBox = QtWidgets.QComboBox(self.loopGroup)
self.huntCmbBox.setGeometry(QtCore.QRect(100, 25, 121, 31))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.huntCmbBox.setFont(font)
self.huntCmbBox.setMouseTracking(False)
self.huntCmbBox.setEditable(False)
self.huntCmbBox.setMaxCount(100)
self.huntCmbBox.setInsertPolicy(QtWidgets.QComboBox.InsertAtBottom)
self.huntCmbBox.setObjectName("huntCmbBox")
self.huntCmbBox.addItem("")
self.HuntinfoLabel = QtWidgets.QLabel(self.loopGroup)
self.HuntinfoLabel.setGeometry(QtCore.QRect(290, 30, 231, 21))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.HuntinfoLabel.setFont(font)
self.HuntinfoLabel.setText("")
self.HuntinfoLabel.setObjectName("HuntinfoLabel")
self.retranslateUi(Dialog)
self.huntCmbBox.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(Dialog)
hnt1 = " 10 - 15 sec. "
hnt2 = " 16 - 20 sec. "
#HUNT
self.huntCmbBox.addItem(hnt1,[10.1, 15.1])
self.huntCmbBox.addItem(hnt2,[16.1, 20.1])
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.stopPushButton.setText(_translate("Dialog", "STOP ALL"))
self.startPushButton.setText(_translate("Dialog", "START"))
self.huntLabel.setText(_translate("Dialog", "Hunt"))
self.huntCmbBox.setCurrentText(_translate("Dialog", " Select Time"))
self.huntCmbBox.setItemText(0, _translate("Dialog", " Select Time"))
self.runHunt = False
self.stop = False
self.startPushButton.clicked.connect(self.start)
self.stopPushButton.clicked.connect(self.stopFunc)
self.stopPushButton.setEnabled(False)
def stopFunc(self):
self.runHunt= False
self.stopPushButton.setEnabled(False)
self.startPushButton.setEnabled(True)
def huntFunction(self):
index1 = self.huntCmbBox.currentIndex()
self.seciliCmBox = self.huntCmbBox.currentData()
self.HuntinfoLabel.clear()
if index1 == 0 and self.runHunt:
self.HuntinfoLabel.clear()
self.HuntinfoLabel.setText(self.HuntinfoLabel.text() + " Error. Stop and select time!")
else:
self.HuntinfoLabel.clear()
self.HuntinfoLabel.setText(self.HuntinfoLabel.text() + " On The Way!")
while True:
if self.runHunt:
randomTimer = float(random.uniform(self.seciliCmBox[0],self.seciliCmBox[1]))
QtTest.QTest.qWait(randomTimer * 1000)
self.HuntinfoLabel.setText("in time runhunt is: "+ str(self.runHunt))
if self.runHunt:
#bla bla code
self.HuntinfoLabel.clear()
self.HuntinfoLabel.setText("in text runhunt is: "+ str(self.runHunt))
def start(self):
self.startPushButton.setEnabled(False)
self.stopPushButton.setEnabled(True)
t1 = threading.Thread(target=self.huntFunction)
if self.huntCheckBox.isChecked():
self.runHunt = True
t1.start()
else:
self.HuntinfoLabel.clear()
self.HuntinfoLabel.setText(self.HuntinfoLabel.text() + " You need check hunt box.")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

How can i change/update label

I am trying to build a simple internet speed test app using pyqt5
I am a beginner and trying to build small apps to learn GUI basics So here is the code whenever i try to change label text from my thread function is says that self has no member named label_7. ignore the print statements i was just using them for debugging
def Update_values(self,MainWindow):
_translate = QtCore.QCoreApplication.translate
self.label_7.setText(_translate("MainWindow", "Download Speed : " + str(down)))
at the upper part of code i got error
Thank you in advance
from PyQt5 import QtCore, QtGui, QtWidgets
import speedtest as st
speed_test = st.Speedtest()
download = speed_test.download()
val = 0
val1 = 0
val3 = 0
down = 0
up = 0
pin = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(331, 307)
font = QtGui.QFont()
font.setFamily("Lucida Sans Unicode")
MainWindow.setFont(font)
MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
MainWindow.setMouseTracking(False)
MainWindow.setTabletTracking(False)
MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
MainWindow.setDocumentMode(False)
MainWindow.setTabShape(QtWidgets.QTabWidget.Triangular)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 3)
self.line_2 = QtWidgets.QFrame(self.centralwidget)
self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.gridLayout.addWidget(self.line_2, 1, 0, 1, 3)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 2)
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label_4.setObjectName("label_4")
self.gridLayout.addWidget(self.label_4, 3, 0, 1, 2)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 4, 0, 1, 2)
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setObjectName("label_7")
self.gridLayout.addWidget(self.label_7, 5, 0, 1, 2)
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setObjectName("label_8")
self.gridLayout.addWidget(self.label_8, 6, 0, 1, 1)
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setProperty("value", 24)
self.progressBar.setTextVisible(False)
self.progressBar.setObjectName("progressBar")
self.gridLayout.addWidget(self.progressBar, 7, 0, 1, 3)
self.check1 = QtWidgets.QCheckBox(self.centralwidget)
self.check1.setObjectName("check1")
self.check1.stateChanged.connect(self.check_1)
self.gridLayout.addWidget(self.check1, 8, 0, 1, 1)
self.check2 = QtWidgets.QCheckBox(self.centralwidget)
self.check2.setObjectName("check2")
self.check2.stateChanged.connect(self.check_2)
self.gridLayout.addWidget(self.check2, 8, 1, 1, 1)
self.check3 = QtWidgets.QCheckBox(self.centralwidget)
self.check3.setObjectName("check3")
self.check3.stateChanged.connect(self.check_3)
self.gridLayout.addWidget(self.check3, 8, 2, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.start_test)
self.gridLayout.addWidget(self.pushButton, 9, 0, 1, 3)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 331, 22))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuAbout = QtWidgets.QMenu(self.menubar)
self.menuAbout.setObjectName("menuAbout")
MainWindow.setMenuBar(self.menubar)
self.actionSave_Results = QtWidgets.QAction(MainWindow)
self.actionSave_Results.setCheckable(False)
self.actionSave_Results.setChecked(False)
self.actionSave_Results.setObjectName("actionSave_Results")
self.actionChange_Save_Directory = QtWidgets.QAction(MainWindow)
self.actionChange_Save_Directory.setObjectName("actionChange_Save_Directory")
self.actionDeveloper = QtWidgets.QAction(MainWindow)
self.actionDeveloper.setObjectName("actionDeveloper")
self.menuFile.addAction(self.actionSave_Results)
self.menuFile.addAction(self.actionChange_Save_Directory)
self.menuAbout.addAction(self.actionDeveloper)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuAbout.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Internet Speed Test"))
self.label.setText(_translate("MainWindow", "Internet Speed Test By FATE"))
self.label_2.setText(_translate("MainWindow", "Status : Connected"))
self.label_4.setText(_translate("MainWindow", "Ping : --"))
self.label_3.setText(_translate("MainWindow", "Upload Speed : --"))
self.label_7.setText(_translate("MainWindow", "Download Speed : --"))
self.label_8.setText(_translate("MainWindow", "Progress :"))
self.check1.setText(_translate("MainWindow", "Download"))
self.check2.setText(_translate("MainWindow", "Upload"))
self.check3.setText(_translate("MainWindow", "Ping"))
self.pushButton.setText(_translate("MainWindow", "Start"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuAbout.setTitle(_translate("MainWindow", "About"))
self.actionSave_Results.setText(_translate("MainWindow", "Save Results"))
self.actionChange_Save_Directory.setText(_translate("MainWindow", "Change Save Directory"))
self.actionDeveloper.setText(_translate("MainWindow", "Developer"))
self.Update_values(MainWindow)
def Update_values(self,MainWindow):
_translate = QtCore.QCoreApplication.translate
self.label_7.setText(_translate("MainWindow", "Download Speed : " + str(down)))
# self.label_7.setText(_translate("MainWindow", "Download Speed : 10"))
print(down)
print(up)
print(pin)
def check_1(self, state):
global val
print(val)
if state != self.check1.isChecked():
val += 1
print("YESH "+ str(val))
else:
val -= 1
print("NO" + str(val))
def check_2(self, state):
global val1
print(val1)
if state != self.check1.isChecked():
val1 += 1
print("YESH "+ str(val1))
else:
val1 -= 1
print("NO" + str(val1))
def check_3(self, state):
global val3
print(val3)
if state != self.check1.isChecked():
val3 += 1
print("YESH "+ str(val3))
else:
val3 -= 1
print("NO" + str(val3))
def start_test(self):
self.threadclass = Thread()
self.threadclass.start()
class Thread(QtCore.QThread,Ui_MainWindow):
def __init__(self,parent = None):
super(Thread, self).__init__(parent)
def run(self):
if val == 1:
download = speed_test.download()
download_mbs = round(download / (10**6), 2)
global down
down = download_mbs
self.Update_values(Ui_MainWindow)
if val1 == 1:
upload = speed_test.upload()
upload_mbs = round(upload / (10**6), 2)
global up
up = upload_mbs
if val3 == 1:
ping = speed_test.results.ping
global pin
pin = ping
print("DONE")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
First of all, you should not modify the code generated by Qt Designer, so in my next solution you must regenerate the .py generated by pyuic and call it gui.py: python -m pyuic5 your_ui.ui -o gui.py.
Another error you have is that you should not update the GUI from another thread, nor use global variables since they are prone to generating silent errors, instead you should use the signals as shown below:
from enum import Flag, auto
import threading
from PyQt5 import QtCore, QtGui, QtWidgets
import speedtest
from gui import Ui_MainWindow
class Task(Flag):
NONE = auto()
DOWNLOAD = auto()
UPLOAD = auto()
PING = auto()
class SpeedtestWrapper(QtCore.QObject):
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
downloaded = QtCore.pyqtSignal(float)
uploaded = QtCore.pyqtSignal(float)
pinged = QtCore.pyqtSignal(float)
def start(self, flag):
threading.Thread(target=self._execute, args=(flag,), daemon=True).start()
def _execute(self, flag):
self.started.emit()
speed_test = speedtest.Speedtest()
if flag & Task.DOWNLOAD:
download = speed_test.download()
download_mbs = round(download / (10 ** 6), 2)
self.downloaded.emit(download_mbs)
if flag & Task.DOWNLOAD:
upload = speed_test.upload()
upload_mbs = round(upload / (10 ** 6), 2)
self.uploaded.emit(upload_mbs)
if flag & Task.DOWNLOAD:
ping = speed_test.results.ping
self.pinged.emit(ping)
self.finished.emit()
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self._flag = Task.NONE
self.speed_wrapper = SpeedtestWrapper()
self.speed_wrapper.started.connect(self.on_started)
self.speed_wrapper.finished.connect(self.on_finished)
self.speed_wrapper.downloaded.connect(self.on_downloaded)
self.speed_wrapper.uploaded.connect(self.on_uploaded)
self.speed_wrapper.pinged.connect(self.on_pinged)
#QtCore.pyqtSlot()
def start_test(self):
if self._flag != Task.NONE:
self.speed_wrapper.start(self._flag)
#QtCore.pyqtSlot(int)
def check_1(self, state):
if state:
self._flag |= Task.DOWNLOAD
else:
self._flag &= ~Task.DOWNLOAD
#QtCore.pyqtSlot(int)
def check_2(self, state):
if state:
self._flag |= Task.UPLOAD
else:
self._flag &= ~Task.UPLOAD
#QtCore.pyqtSlot(int)
def check_3(self, state):
if state:
self._flag |= Task.PING
else:
self._flag &= ~Task.PING
#QtCore.pyqtSlot()
def on_started(self):
self.label_7.setText(self.tr("Download Speed: --"))
self.label_3.setText(self.tr("Upload Speed: --"))
self.label_4.setText(self.tr("Ping Speed: --"))
self.pushButton.setEnabled(False)
#QtCore.pyqtSlot()
def on_finished(self):
self.pushButton.setEnabled(True)
#QtCore.pyqtSlot(float)
def on_downloaded(self, download):
self.label_7.setText(self.tr("Download Speed: %f" % download))
#QtCore.pyqtSlot(float)
def on_uploaded(self, upload):
self.label_3.setText(self.tr("Upload Speed: %f" % upload))
#QtCore.pyqtSlot(float)
def on_pinged(self, ping):
self.label_4.setText(self.tr("Ping Speed: %f" % ping))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Pyqt4 error messages: Unexpected type 'str' and not enough arguments

I am getting a
TypeError: arguments did not match any overloaded call:"
message when I run my Pyqt4 program that is supposed to invoke other mini-windows when the add, edit or delete menu items are clicked, What am I doing wrong
The exact error message is
Traceback (most recent call last):
File "C:/Python27/Lib/site-packages/PyQt4/mytry.py", line 27, in <module>
add=QAction("Add new record")
TypeError: arguments did not match any overloaded call:
QAction(QObject): argument 1 has unexpected type 'str'
QAction(QString, QObject): not enough arguments
QAction(QIcon, QString, QObject): argument 1 has unexpected type 'str'
here is mytry.py
import sys
from PyQt4.QtGui import *
# Create an PyQT4 application object.
a = QApplication(sys.argv)
# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QMainWindow()
# Set window size.
w.resize(320, 240)
# Set window title
w.setWindowTitle("Hello World!")
# Create main menu
mainMenu = w.menuBar()
mainMenu.setNativeMenuBar(False)
fileMenu = mainMenu.addMenu('File')
dataMaintMenu = mainMenu.addMenu('Data Maintenance')
reportingMenu = mainMenu.addMenu('Reporting')
aboutMenu = mainMenu.addMenu('About')
# Create Add menu
add=QAction("Add new record")
dataMaintMenu.addAction(add)
dataMaintMenu.triggered[QAction].connect(self.openAddWindow)
self.setLayout(layout)
# Create Edit menu
edit=QAction("Edit new record")
dataMaintMenu.addAction(edit)
dataMaintMenu.triggered[QAction].connect(self.openEditWindow)
self.setLayout(layout)
# Create Delete menu
delete=QAction("Delete new record")
dataMaintMenu.addAction(delete)
dataMaintMenu.triggered[QAction].connect(self.openDeleteWindow)
self.setLayout(layout)
# Add exit button
exitButton = QAction(QIcon('exit24.png'), 'Exit', w)
exitButton.setShortcut('Ctrl+Q')
exitButton.setStatusTip('Exit application')
exitButton.triggered.connect(w.close)
fileMenu.addAction(exitButton)
def openAddWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_AddWindow()
self.ui.setupUi(self.window)
MainWindow.hide()
self.window.show()
def openEditWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_EditWindow()
self.ui.setupUi(self.window)
MainWindow.hide()
self.window.show()
def openDeleteWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DeleteWindow()
self.ui.setupUi(self.window)
MainWindow.hide()
self.window.show()
# Show window
w.show()
sys.exit(a.exec_())
and i have also just included only my Add.py file and not the delete or edit
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_AddWindow(object):
def setupUi(self, AddWindow):
AddWindow.setObjectName(_fromUtf8("AddWindow"))
AddWindow.resize(653, 424)
self.centralwidget = QtGui.QWidget(AddWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 30, 301, 31))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(50, 80, 101, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(50, 120, 181, 16))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.label_4 = QtGui.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(50, 170, 171, 16))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.label_5 = QtGui.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(50, 210, 211, 16))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.label_6 = QtGui.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(50, 240, 161, 16))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.label_7 = QtGui.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(50, 270, 161, 16))
self.label_7.setObjectName(_fromUtf8("label_7"))
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(260, 79, 331, 21))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.lineEdit_2 = QtGui.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(260, 120, 201, 20))
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.lineEdit_3 = QtGui.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(260, 170, 191, 20))
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
self.lineEdit_4 = QtGui.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(260, 240, 191, 20))
self.lineEdit_4.setObjectName(_fromUtf8("lineEdit_4"))
self.comboBox = QtGui.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(260, 210, 361, 22))
self.comboBox.setObjectName(_fromUtf8("comboBox"))
self.comboBox_2 = QtGui.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(260, 270, 301, 22))
self.comboBox_2.setObjectName(_fromUtf8("comboBox_2"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(160, 300, 231, 71))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
AddWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(AddWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 653, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
AddWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(AddWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
AddWindow.setStatusBar(self.statusbar)
self.retranslateUi(AddWindow)
QtCore.QMetaObject.connectSlotsByName(AddWindow)
def retranslateUi(self, AddWindow):
AddWindow.setWindowTitle(_translate("AddWindow", "MainWindow", None))
self.label.setText(_translate("AddWindow", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">Register your details</span></p></body></html>", None))
self.label_2.setText(_translate("AddWindow", "Name and Surname", None))
self.label_3.setText(_translate("AddWindow", "Unisa Student number (if applicable)", None))
self.label_4.setText(_translate("AddWindow", "RSA Identity Number", None))
self.label_5.setText(_translate("AddWindow", "Community Project you are interested In", None))
self.label_6.setText(_translate("AddWindow", "Contact Cell or Tel Number", None))
self.label_7.setText(_translate("AddWindow", "Nearest Unisa Regional Office", None))
self.pushButton.setText(_translate("AddWindow", "DONE", None))
Look at this line:
add=QAction("Add new record")
As your error says, there is no constructor for QAction that accepts only one string. Add parent parameter (I recommend to use dataMaintMenu or None as parent):
add = QAction("Add new record", dataMaintMenu)

AttributeError: 'Ui_Login' object has no attribute 'GetTable'

Im creating a login and signup page that links to other GUI using PyQt. Most of the code works but when i try to sign up a new user, it gives me a AttributeError: 'Ui_Login' object has no attribute 'GetTable'. GetTable is defined in the code for the databse created with MySQl whic is called into the Login class and Signup class.
Please in new to python and programming in general. Sorry if this question seems daft. but i have read a lot on previously asked ones and i cant seem to understand what it is saying. Thanks
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Login.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
import sys
import DBmanager as db
from PyQt4 import QtCore, QtGui
from newuser import Ui_Form
from createprofile import Ui_StudentLogin
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)
#######SIGN IN/ LOG IN#################################################################################################
class Ui_Login(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.dbu = db.DatabaseUtility('UsernamePassword_DB', 'masterTable')
self.setupUi(self)
self.confirm = None
def setupUi(self, Login):
Login.setObjectName(_fromUtf8("Form"))
Login.resize(400, 301)
self.label = QtGui.QLabel(Login)
self.label.setGeometry(QtCore.QRect(60, 60, 71, 21))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Login)
self.label_2.setGeometry(QtCore.QRect(60, 120, 81, 21))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.userName = QtGui.QLineEdit(Login)
self.userName.setGeometry(QtCore.QRect(140, 60, 151, 21))
self.userName.setObjectName(_fromUtf8("userName"))
self.passWord = QtGui.QLineEdit(Login)
self.passWord.setGeometry(QtCore.QRect(140, 120, 151, 21))
self.passWord.setObjectName(_fromUtf8("passWord"))
self.label_3 = QtGui.QLabel(Login)
self.label_3.setGeometry(QtCore.QRect(160, 10, 131, 21))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.loginButton1 = QtGui.QPushButton(Login)
self.loginButton1.setGeometry(QtCore.QRect(40, 210, 75, 23))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.loginButton1.setFont(font)
self.loginButton1.setObjectName(_fromUtf8("loginButton1"))
self.loginButton1.clicked.connect(self.login_Button1)
self.signUpButton = QtGui.QPushButton(Login)
self.signUpButton.setGeometry(QtCore.QRect(160, 210, 75, 23))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.signUpButton.setFont(font)
self.signUpButton.setObjectName(_fromUtf8("signUpButton"))
self.signUpButton.clicked.connect(self.signUp_Button)
self.cancel1 = QtGui.QPushButton(Login)
self.cancel1.setGeometry(QtCore.QRect(280, 210, 75, 23))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.cancel1.setFont(font)
self.cancel1.setObjectName(_fromUtf8("cancel1"))
self.connect(self, QtCore.SIGNAL('triggered()'), self.cancel_1)
self.retranslateUi(Login)
QtCore.QMetaObject.connectSlotsByName(Login)
def retranslateUi(self, Login):
Login.setWindowTitle(_translate("Form", "Login", None))
self.label.setText(_translate("Form", "USERNAME", None))
self.label_2.setText(_translate("Form", "PASSWORD", None))
self.label_3.setText(_translate("Form", "LOGIN PAGE", None))
self.loginButton1.setText(_translate("Form", "LOGIN", None))
self.signUpButton.setText(_translate("Form", "SIGN UP", None))
self.cancel1.setText(_translate("Form", "CANCEL", None))
#QtCore.pyqtSignature("on_cancel1_clicked()")
def cancel_1(self):
self.close()
#QtCore.pyqtSignature("on_loginButton1_clicked()")
def login_Button1(self):
username = self.userName.text()
password = self.passWord.text()
if not username:
QtGui.QMessageBox.warning(self, 'Guess What?', 'Username Missing!')
elif not password:
QtGui.QMessageBox.warning(self, 'Guess What?', 'Password Missing!')
else:
self.AttemptLogin(username, password)
def AttemptLogin(self, username, password):
t = self.dbu.GetTable()
print (t)
for col in t:
if username == col[1]:
if password == col[2]:
QtGui.QMessageBox.information(self, 'WELCOME', 'Success!!')
self.createProfilePage()
self.close()
else:
QtGui.QMessageBox.warning(self, 'OOPS SORRY!', 'Password incorrect...')
return
def createProfilePage(self):
self.createprofile = Ui_StudentLogin()
self.createprofile.show()
#QtCore.pyqtSignature("on_signUpButton_clicked()")
def signUp_Button(self):
self.newuser = Ui_Form(self)
self.newuser.show()
#######SIGN UP/ CREATE NEW USER#################################################################################################
class Ui_Form(QtGui.QWidget):
def __init__(self,dbu):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.dbu = dbu
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(60, 70, 51, 16))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.nameGet = QtGui.QLineEdit(Form)
self.nameGet.setGeometry(QtCore.QRect(120, 70, 191, 21))
self.nameGet.setObjectName(_fromUtf8("nameGet"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(50, 120, 46, 13))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.label_3 = QtGui.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(30, 170, 71, 16))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.regNoGet = QtGui.QLineEdit(Form)
self.regNoGet.setGeometry(QtCore.QRect(120, 120, 191, 21))
self.regNoGet.setObjectName(_fromUtf8("regNoGet"))
self.passWordGet = QtGui.QLineEdit(Form)
self.passWordGet.setGeometry(QtCore.QRect(120, 170, 191, 21))
self.passWordGet.setObjectName(_fromUtf8("passWordGet"))
self.label_4 = QtGui.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(100, 20, 181, 21))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.nextButton = QtGui.QPushButton(Form)
self.nextButton.setGeometry(QtCore.QRect(140, 250, 75, 23))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.nextButton.setFont(font)
self.nextButton.setObjectName(_fromUtf8("nextButton"))
self.nextButton.clicked.connect(self.next_Button)
self.cancelButton = QtGui.QPushButton(Form)
self.cancelButton.setGeometry(QtCore.QRect(260, 250, 75, 23))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.cancelButton.setFont(font)
self.cancelButton.setObjectName(_fromUtf8("cancelButton"))
self.cancelButton.clicked.connect(self.cancel_Button)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "New User", None))
self.label.setText(_translate("Form", "NAME", None))
self.label_2.setText(_translate("Form", "REG. NO", None))
self.label_3.setText(_translate("Form", "PASSWORD", None))
self.label_4.setText(_translate("Form", " CREATE NEW USER", None))
self.nextButton.setText(_translate("Form", "SUBMIT", None))
self.cancelButton.setText(_translate("Form", "CANCEL", None))
#QtCore.pyqtSignature("on_cancelButton_clicked()")
def cancel_Button(self):
self.close()
#QtCore.pyqtSignature("on_nextButton_clicked()")
def next_Button(self):
username = self.nameGet.text()
password = self.passWordGet.text()
regno = self.regNoGet.text()
if not username:
QtGui.QMessageBox.warning(self, 'Warning', 'Username Missing')
elif not password:
QtGui.QMessageBox.warning(self, 'Warning!', 'Password Missing')
elif not regno:
QtGui.QMessageBox.warning(self, 'Warning!', 'RegNo Missing')
else:
t = self.dbu.GetTable()
print (t)
for col in t:
if username == col[1]:
QtGui.QMessageBox.warning(self, 'Dang it!', 'Username Taken. :(')
else:
self.dbu.AddEntryToTable (username, password, regno)
QtGui.QMessageBox.information(self, 'Awesome!!', 'User Added SUCCESSFULLY!')
self.close()
## def createProfileWindow(self):
## self.createprofile = Ui_StudentLogin()
## self.createprofile.show()
##
## def generate_report(self):
## data_line1 = self.nameGet.displayText()
## data_line2 = self.regNoGet.displayText()
## data_line3 = self.passWordGet.displayText()
## print data_line1
## print data_line2
## print data_line3
##
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Ui_Login()
ex.show()
sys.exit(app.exec_())
The issue seems to be in your initialization of the Ui_Form. In this snippet
#QtCore.pyqtSignature("on_signUpButton_clicked()")
def signUp_Button(self):
self.newuser = Ui_Form(self)
self.newuser.show()
it looks like you are passing self, in this case an instance of the Ui_Login class, as the dbu to the constructor of the `Ui_Form' class. Instead you should pass the dbu like so.
#QtCore.pyqtSignature("on_signUpButton_clicked()")
def signUp_Button(self):
self.newuser = Ui_Form(self.dbu)
self.newuser.show()
You will find a lot of code that doesn't call out keyword arguments in constructors like this, but I find it to be bad practice in maintaining a readable codebase. So I would advise you to do something like this in future to help you avoid confusion.
self.newuser = Ui_Form(dbu=self.dbu)
Your code expects this line:
db.DatabaseUtility('UsernamePassword_DB', 'masterTable')
to return an object with a GetTable method (and assign that object to self.dbu). Therefore when you call self.dbu.GetTable() you get an error if whatever was returned from:
db.DatabaseUtility('UsernamePassword_DB', 'masterTable')
doesn't have that method. So check what:
db.DatabaseUtility('UsernamePassword_DB', 'masterTable')
actually returns and adjust your code accordingly.

Drag and Drop between two QTreeViews

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,

Categories