Not sure if I get what you mean, but you can simply provide the initial text in creating the LineEdits like self.page1Layout.addRow("Name:", QLineEdit('write some text here'))
Yes, something similar to that. But I want it as placeholder text, since when the focus moves on to that particular linedit() we can enter the data (and automatically vanish the previously entered text - placeholder text).
But here in this case text will be displayed, so you have to remove the text manually and then enter the new text…
But whereas setPlaceholderText("enter text") will be displayed as shadow text and not required to remove that text before entering actual text.
Can it be possible to change the title of the window dynamically, means once I select/change the page from page1 to page2 the title of the window should be changed and vice versa as well.
Like below
when we select page1 from the above combo box, then the title of the window should be
This is MyPage1
similarly when we select the page2 from the combo box, it should be displayed like
This is MyPage2.
Any suggestion would be appreciated. Tahnk you.
Regards,
maiya
You’re setting the WindowTitle of the stacked widget now, but the title of the mainwidget is shown. To be able to set that from within the stacked widget, you need to create it with the mainwindow as parent.
change main = Stacked()
to main = Stacked(self)
and
class Stacked:
def __init__(self):
super().__init__()
to
class Stacked:
def __init__(self, parent):
super().__init__(parent)
then you can call setWindowTitle on self.parent() instead of on self.
Here you go tested on PyQt6 for python 3.12.
Notice that you have to call setCentralWidget.
And I’m passing the mainwindow does as the parent of the widget.
import sys
from PyQt6 import QtWidgets
from PyQt6.QtWidgets import (
QApplication,
QComboBox,
QFormLayout,
QLineEdit,
QStackedLayout,
QVBoxLayout,
QWidget,
QMainWindow,
)
class Stacked(QWidget):
def __init__(self, parent):
super().__init__(parent)
parent.setWindowTitle("QStackedLayout Example")
# Create a top-level layout
layout = QVBoxLayout()
self.setLayout(layout)
# Create and connect the combo box to switch between pages
self.pageCombo = QComboBox()
self.pageCombo.addItems(["Page 1", "Page 2"])
self.pageCombo.activated.connect(self.switchPage)
# Create the stacked layout
self.stackedLayout = QStackedLayout()
# Create the first page
self.page1 = QWidget()
self.page1Layout = QFormLayout()
self.page1Layout.addRow("Name:", QLineEdit())
self.page1Layout.addRow("Address:", QLineEdit())
self.page1.setLayout(self.page1Layout)
self.stackedLayout.addWidget(self.page1)
# Create the second page
self.page2 = QWidget()
self.page2Layout = QFormLayout()
self.page2Layout.addRow("Job:", QLineEdit())
self.page2Layout.addRow("Department:", QLineEdit())
self.page2.setLayout(self.page2Layout)
self.stackedLayout.addWidget(self.page2)
# Add the combo box and the stacked layout to the top-level layout
layout.addWidget(self.pageCombo)
layout.addLayout(self.stackedLayout)
def switchPage(self):
self.stackedLayout.setCurrentIndex(self.pageCombo.currentIndex())
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.stacked = Stacked(self)
self.setCentralWidget(self.stacked)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())