I need your help!
The situation: I have different tabs and in one tab ["Support"-Tab] I want to use the QWebView Widget.
But the site should first load when I click on this tab:
main.py
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
from tab-file import Support
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.createTabs()
widgetLayout = QtGui.QVBoxLayout()
widgetLayout.addWidget(self.tabs)
self.setLayout(widgetLayout)
self.setWindowTitle("Tabs")
self.resize(400,400)
def createTabs(self):
self.tabs = QtGui.QTabWidget()
support = Support()
tab1 = QtGui.QWidget()
tab2 = support
self.tabs.addTab(tab1,"tab1")
self.tabs.addTab(tab2,"SUPPORT")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
I load the "Support"-Tab from this file:
tab-file.py
import sys
from PyQt4 import QtGui, QtCore, QtWebKit
class Support(QtGui.QWidget):
def __init__(self, parent=None):
super(Support, self).__init__(parent)
self.supportTab()
def supportTab(self):
view = QtWebKit.QWebView()
url = "http://www.google.com"
view.load(QtCore.QUrl(url))
vbox = QtGui.QVBoxLayout()
vbox.addWidget(view)
self.setLayout(vbox)
Can you please tell me how I can solve this?
Thanks in advance.
in main.py:
make the tab2 addressable, and add code to listen to tab change event:
#...
self.tab2 = support
self.tabs.addTab(tab1,"tab1")
self.tabs.addTab(self.tab2,"SUPPORT")
#
self.tabs.currentChanged.connect(self.load_on_show)
Then add the action
def load_on_show(self):
idx = self.tabs.currentIndex()
if idx == 1:
url = "http://www.google.com"
print url
self.tab2.load_url(url)
At last, in tab_file.py [i can't use a dash, have to use an underscore!]:
Make view addressable, again (self.view) and add code
def load_url(self, url):
self.view. load(QtCore.QUrl(url))
Does this help?
Related
I have a Tab Widget with 2 Tabs. How can I get the index number of a Tab when I click on it?
self.tab = QTabWidget()
self.tab.addTab(self.widget1, "Tab1")
self.tab.addTab(self.widget2, "Tab2")
data_entry_tab.tabBarClicked.connect(self.OnTabClicked)
def OnTabClicked(self):
tab_bar = self.sender()
# I want to use idx to do some calculation
idx = ?
You can use the tabBarClicked signal:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.tabwidget = QtWidgets.QTabWidget()
self.tabwidget.addTab(QtWidgets.QWidget(), "Tab1")
self.tabwidget.addTab(QtWidgets.QWidget(), "Tab2")
self.tabwidget.addTab(QtWidgets.QWidget(), "Tab3")
self.setCentralWidget(self.tabwidget)
self.tabwidget.tabBarClicked.connect(self.handle_tabbar_clicked)
def handle_tabbar_clicked(self, index):
print(index)
print("x2:", index * 2)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
whatever you have your Qtabwidget set up as, like lets say. tabs = QTabWidget(), for instance. then you would use tabs.currentIndex() to find the index of the tab that is currently open. so then you can be like current_index = tabs.currentIndex(), and use the index for whatever it is you were needing it for.
I read that it is possible to use a simple QWidget as Layout for a page of a QTabWidget, so that I can design the tab page the way I want it.
How do I implement that?
One of the functionalities of a QWidget is to be a container so it is only necessary to add it in each tab:
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.tab_widget = QtWidgets.QTabWidget()
self.setCentralWidget(self.tab_widget)
page1 = QtWidgets.QWidget()
page2 = QtWidgets.QWidget()
for page, title in ((page1, "Page1"), (page2, "Page2")):
self.tab_widget.addTab(page, title)
lay1 = QtWidgets.QVBoxLayout(page1)
for i in range(3):
lay1.addWidget(
QtWidgets.QLabel(f"label{i}", alignment=QtCore.Qt.AlignCenter)
)
lay1.addStretch()
lay2 = QtWidgets.QVBoxLayout(page2)
for i in range(4):
lay2.addWidget(QtWidgets.QPushButton(f"button {i}"))
lay2.addStretch()
self.resize(640, 480)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
I want to save/change my 'QLineEdit' text in my app and get again by using 'QPushButton' on some specific position.
The logic is to associate the information with a key, in the following example I show how the text is saved when the text is modified and then the text is retrieved by pressing the button.
from PySide2 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.m_le = QtWidgets.QLineEdit()
self.m_le.textChanged.connect(self.onTextChanged)
self.m_button = QtWidgets.QPushButton("Press Me")
self.m_button.clicked.connect(self.onClicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.m_le)
lay.addWidget(self.m_button)
#QtCore.Slot(str)
def onTextChanged(self, text):
settings = QtCore.QSettings()
settings.setValue("text", text)
#QtCore.Slot()
def onClicked(self):
settings = QtCore.QSettings()
text = settings.value("text")
print(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
I'm totally new to PyQt5 and I'm trying to develop an application with a main menu and sub menus and i don't know how to start. All examples that I found are using that pop up windows as sub menu.
Here is an example of what I'm going to do. Any suggestion on how to start and what to use in PyQt5 please ?
The main menu is on the right and a sub menu is on the left
To make the main menu you can use QListWidget in the icon mode, and to exchange the pages you can use QStackedLayout:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Page(QtWidgets.QWidget):
clicked = QtCore.pyqtSignal()
def __init__(self, content, parent=None):
super(Page, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
button = QtWidgets.QPushButton("Back")
button.clicked.connect(self.clicked)
lay.addWidget(button)
lay.addWidget(content)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
self.stacked = QtWidgets.QStackedWidget()
lay.addWidget(self.stacked)
self.listWidget = QtWidgets.QListWidget()
self.listWidget.setViewMode(QtWidgets.QListView.IconMode)
self.listWidget.setIconSize(QtCore.QSize(128, 128))
self.listWidget.itemClicked.connect(self.onItemClicked)
self.stacked.addWidget(self.listWidget)
for icon, content in [("alarms.png", QtWidgets.QTextEdit() ),
("fitness.png", QtWidgets.QMdiArea() ),
("navigation.png", QtWidgets.QTableWidget(4, 4) ),
("notifications.png", QtWidgets.QListWidget() )]:
p = Page(content)
p.clicked.connect(lambda: self.stacked.setCurrentIndex(0))
self.addPage(QtGui.QIcon(icon), p)
def addPage(self, icon, widget):
it = QtWidgets.QListWidgetItem()
it.setIcon(icon)
self.listWidget.addItem(it)
self.stacked.addWidget(widget)
it.setData(QtCore.Qt.UserRole, self.listWidget.count())
def onItemClicked(self, it):
ix = it.data(QtCore.Qt.UserRole)
self.stacked.setCurrentIndex(ix)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
I have a QVBoxLayout where I put some buttons. I wrote a function to remove a button, but when I do it, the box doesn't adapt its size to the content.
Here is a piece of the removal function:
for each_difference in differences_remove:
old_index = self.all_tags.index(each_difference)
print("old" + str(old_index))
self.vbox_all_tags.removeWidget(self.liste_pressoirs[old_index])
del self.liste_pressoirs[old_index]
I would like self.vbox_all_tags to adapt its size to the new content after I remove a button. How would you do that ?
Kindly.
Just call adjustSize on your widget after removing the button, here is a demonstration:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
_buttons = []
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.pushButtonRemove = QtGui.QPushButton(self)
self.pushButtonRemove.setText("Remove A Button!")
self.pushButtonRemove.clicked.connect(self.on_pushButtonRemove_clicked)
self.widgetButtons = QtGui.QWidget(self)
self.layoutButtons = QtGui.QHBoxLayout(self.widgetButtons)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.pushButtonRemove)
self.layout.addWidget(self.widgetButtons)
for buttonNumber in range(3):
pushButton = QtGui.QPushButton()
pushButton.setText("Button {0}".format(buttonNumber))
self._buttons.append(pushButton)
self.layoutButtons.addWidget(pushButton)
#QtCore.pyqtSlot()
def on_pushButtonRemove_clicked(self):
if self._buttons:
pushButton = self._buttons[-1]
self._buttons.pop()
self.layoutButtons.removeWidget(pushButton)
pushButton.deleteLater()
self.widgetButtons.adjustSize()
self.adjustSize()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())