I'm trying to build a little app that loads several webpages in an QTabWidget. This already works well. Now I want the tabs / QWebViews to be reloaded when the current Tab is changed.
I think that there is a problem connection the function "onChange" to the currentChanged-Event.
This is my Code:
#!/usr/bin/env python
import sys, os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebView
class BaseWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.centralWidget = QtGui.QWidget()
self.resize(800, 500)
self.setWindowTitle('Test')
self.tabs = QTabWidget()
#self.tabs.connect(self.tabs,SIGNAL("currentChanged(int)"),self,SLOT("onChange(int)")) #tabs,SLOT("tabChangedSlot(int)")
#self.tabs.currentChanged.connect(self.onChange)
self.webview = QWebView()
self.webview.load(QUrl("http://gmx.de"))
self.webview2 = QWebView()
self.webview2.load(QUrl("http://web.de"))
centralLayout = QtGui.QVBoxLayout()
centralLayout.addWidget(self.tabs, 1)
self.tabs.addTab(self.webview, "gmx")
self.tabs.addTab(self.webview2, "web")
self.centralWidget.setLayout(centralLayout)
self.setCentralWidget(self.centralWidget)
##pyqtSlot()
def onChange(self):
QtGui.QMessageBox.information(self,
"Tab Index Changed!",
"Current Tab Index: ");
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = BaseWindow()
window.show()
sys.exit(app.exec_())
I hope that you can help me solving my problem! Thanks a lot!
Check the changes needed on your code here:
import sys, os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtWebKit import QWebView
class BaseWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.centralWidget = QtGui.QWidget()
self.resize(800, 500)
self.setWindowTitle('Test')
self.tabs = QTabWidget()
self.tabs.blockSignals(True) #just for not showing the initial message
self.tabs.currentChanged.connect(self.onChange) #changed!
self.webview = QWebView()
self.webview.load(QUrl("http://gmx.de"))
self.webview2 = QWebView()
self.webview2.load(QUrl("http://web.de"))
centralLayout = QtGui.QVBoxLayout()
centralLayout.addWidget(self.tabs, 1)
self.tabs.addTab(self.webview, "gmx")
self.tabs.addTab(self.webview2, "web")
self.centralWidget.setLayout(centralLayout)
self.setCentralWidget(self.centralWidget)
self.tabs.blockSignals(False) #now listen the currentChanged signal
##pyqtSlot()
def onChange(self,i): #changed!
QtGui.QMessageBox.information(self,
"Tab Index Changed!",
"Current Tab Index: %d" % i ) #changed!
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = BaseWindow()
window.show()
sys.exit(app.exec_())
Related
I have a form with a TableView and a function that handles refreshing buttons for adding and removing data in the table etc.
I want to update the states of actions in contextmenu:
I can with the mouse events (tableWidget.clicked) but not with the KeyPressEvent (Up and Down)
I tried with the table keyPressEvent event, but for some reason the keys no longer work.
How to intercept the up and down keys of the table to update buttons for example.
here after an extract of the code :
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QModelIndex, QVariant, QAbstractItemModel, QEvent
from PyQt5.QtGui import QFont, QKeyEvent
import numpy as np
from modules import *
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setObjectName("MainWindow")
self.setMinimumSize(QtCore.QSize(1024, 860))
self.setMaximumSize(QtCore.QSize(2400, 1024))
self.setStyleSheet("background-color: rgb(240, 240, 240);")
self.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
self.centralWidget = QtWidgets.QWidget(self)
self.gridLayout = QtWidgets.QGridLayout(self.centralWidget)
self.setCentralWidget(self.centralWidget)
self.retranslateUi()
self.connectActions()
self.tableWidget.customContextMenuRequested.connect(self.context_menu_Show)
def bpuHadChanged(self, changed=False):
self.bpuChanged = changed
self.actionSave.setEnabled(changed)
def saveFile(self):
# TODO
self.actionSave.setEnabled(False)
def setBPU(self, bpu):
self.bpu = bpu
self.model = TableModel(self.bpu, bpuNewLine)
self.tableWidget.setModel(self.model)
self.tableWidget.resizeColumnToContents(0)
self.updateActionsStates()
def connectActions(self):
self.actionNew.triggered.connect(self.newFile)
self.actionQuit.triggered.connect(self.quit)
self.actionAdd_une_ligne_bordereau.triggered.connect(self.addLine)
self.actionRemoveSelectedLine.triggered.connect(self.RemoveLigne)
self.actionInsertLine.triggered.connect(self.insertLine)
self.actionSave.triggered.connect(self.saveFile)
self.tableWidget.clicked.connect(self.updateActionsStates)
# TODO add connections for up et down actions
def context_menu_Show(self):
self.updateActionsStates()
cursor = QtGui.QCursor()
self.contextMenu.exec_(cursor.pos())
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.setBPU(bpu)
window.show()
app.exec_()
I'm trying to build a date printer using Pyqt5 QDateEdit. I can popup the calendar, but I want to write the clicked date's string in the console (or in a label in window). I tried print(self.calendarWidget().document().toPlainText()) or print(self.calendarWidget().currentText()) but it didn't work.
I use this code;
from PyQt5 import QtCore, QtWidgets
class DateEdit(QtWidgets.QDateEdit):
popupSignal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(DateEdit, self).__init__(parent)
self.setCalendarPopup(True)
self.calendarWidget().installEventFilter(self)
def eventFilter(self, obj, event):
if self.calendarWidget() is obj and event.type() == QtCore.QEvent.Show:
self.popupSignal.emit()
return super(DateEdit, self).eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = DateEdit()
w.popupSignal.connect(lambda: print("popup"))
w.show()
sys.exit(app.exec_())
What is its syntax? I didn't find enough documentation for it. Can you help please?
EDIT: The answer
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.dateEdit = QDateEdit(self)
self.lbl = QLabel()
self.dateEdit.setMaximumDate(QtCore.QDate(7999, 12, 28))
self.dateEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
self.dateEdit.setCalendarPopup(True)
layout = QGridLayout()
layout.addWidget(self.dateEdit)
layout.addWidget(self.lbl)
self.setLayout(layout)
self.dateEdit.dateChanged.connect(self.onDateChanged)
def onDateChanged(self, qDate):
print('{0}/{1}/{2}'.format(qDate.day(), qDate.month(), qDate.year()))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())
I want to show the results by clicking on the button, but if I press this code, the program will end in two seconds.
And 'pursent.ui' is just a widget that hasn't been set up.
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("pursent.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.btneve)
def btneve(self):
self.statusbar.showMessage((int(self.lineEdit_2.text())-int(self.lineEdit.text()))/int(self.lineEdit.text())*100)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
void QStatusBar::showMessage(const QString &message, int timeout = 0)
Hides the normal status indications and displays the given message for the specified number of milli-seconds (timeout).
import sys
from PyQt5.QtWidgets import *
#from PyQt5 import uic
#form_class = uic.loadUiType("pursent.ui")[0]
class MyWindow(QMainWindow): #, form_class):
def __init__(self):
super().__init__()
# self.setupUi(self)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.lineEdit = QLineEdit()
self.lineEdit_2 = QLineEdit()
self.pushButton = QPushButton('Button')
self.pushButton.clicked.connect(self.btneve)
self.statusbar = self.statusBar() # = StatusBar(self)
grid = QGridLayout(centralWidget)
grid.addWidget(self.lineEdit)
grid.addWidget(self.lineEdit_2)
grid.addWidget(self.pushButton)
def btneve(self):
self.statusbar.showMessage(str( # + str
(int(self.lineEdit_2.text())-int(self.lineEdit.text()))/int(self.lineEdit.text())*100)
)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
Hi i have posted the code below through which i'm unable to display label in pyqt4. Any suggestions would be helpful .
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
class Entry_view(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(25, 25, 800, 480)
label = QtGui.QLabel()
label.setText("Welcome To Python GUI")
label.resize(100, 50)
# label.show(self)
self.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = Entry_view()
sys.exit(app.exec_())
You did not keep a reference to the label, so it got garbage-collected before it could be shown. Try this instead:
self.label = QtGui.QLabel(self)
self.label.setText("Welcome To Python GUI")
self.label.resize(100, 50)
Why did you set the text but don't process the app using thus:
app.processEvents() # On the QApplication.
Or just do:
label = QtGui.QLabel(text="Welcome to Python GUI!")
Or:
label = QtGui.QLabel("Welcomme To Python GUI!")
Or another way is:
label.show() # No widgets on it.
code below is the solution ,
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
class Entry_view(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(25, 25, 800, 480)
label = QtGui.QLabel()
label.setText("Swipe The Card")
vbox = QtGui.QVBoxLayout()
label.setAlignment(Qt.AlignCenter)
vbox.addWidget(label)
vbox.addStretch()
self.setLayout(vbox)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = Entry_view()
sys.exit(app.exec_())
Is there a way in Qt5 to apply a QGraphicsEffect to a widget, even when one of its parent widgets already has a QGraphicsEffect applied to it?
When i try the following:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class ParentWidget(QWidget):
def __init__(self,parent):
super().__init__(parent)
effect = QGraphicsBlurEffect(self)
self.setGraphicsEffect(effect)
effect.setBlurRadius(0)
class ChildWidget(ParentWidget):
def __init__(self,parent):
super().__init__(parent)
self.layout = QGridLayout(self)
widget = QWidget()
widget.setObjectName('reviewControlArea')
effect = QGraphicsOpacityEffect(widget)
widget.setGraphicsEffect(effect)
self.layout.addWidget(widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = QMainWindow()
cw = ChildWidget(MainWindow)
MainWindow.setCentralWidget(cw)
MainWindow.show()
sys.exit(app.exec_())
The stdout says:
QPainter::begin: A paint device can only be painted by one painter at a time.