How do I disable higlighting selected items in QListWidget pyqt5?
Tried the following which didn't work:
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem, QGridLayout, QLabel, QWidget
app = QApplication([])
widget = QWidget()
listWidget = QListWidget()
item = QListWidgetItem('Pogba', listWidget)
layout.addWidget(listWidget)
#Attempt
palette = QPalette()
palette.setColor(QPalette.Highlight, listWidget.palette().color(QPalette.Base))
palette.setColor(QPalette.HighlightedText, listWidget.palette().color(QPalette.Text))
listWidget.setPalette(palette)
widget.setLayout(layout)
widget.show()
app.exec()
And the following only makes the item grey instead of blue:
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem, QGridLayout, QLabel, QWidget
app = QApplication([])
widget = QWidget()
listWidget = QListWidget()
item = QListWidgetItem('Pogba', listWidget)
layout.addWidget(listWidget)
#Other attempt
listWidget.setFocusPolicy(Qt.NoFocus)
widget.setLayout(layout)
widget.show()
app.exec()
Is there another way to achieve this?
Related
I'm trying to integrate QFontComboBox within Qmenu.
I try to make two things happen when selecting a particular font from the menu:
The Qmenu will close.
print the selected font.
from PyQt5.QtCore import QObject
from PyQt5.QtGui import QIcon, QFont, QCursor
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QFontComboBox, QWidgetAction, QMenu, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Pyside2 FontCombo Box")
self.setGeometry(300,200,300,250)
self.setFontBox()
self.setIcon()
self.show()
def setIcon(self):
appIcon = QIcon("icon.png")
self.setWindowIcon(appIcon)
def setFontBox(self):
self.font_button = QPushButton(self)
self.font_button.setFixedWidth(300)
self.font_button.clicked.connect(lambda: self.setFontmenu())
vbox = QVBoxLayout()
vbox.addWidget(self.font_button)
self.setLayout(vbox)
def setFontmenu(self):
font_menu = QMenu()
font_submenu = QFontComboBox()
font_submenu.setCurrentFont(QFont("Arial"))
objectTest = QObject()
widget = QWidgetAction(objectTest)
widget.setDefaultWidget(font_submenu)
font_menu.addAction(widget)
font_menu.exec_(QCursor.pos())
menu = font_menu
menu.addSeparator()
font_submenu.showPopup()
font_submenu.setFocus()
font_submenu.currentFontChanged.connect(self._changed)
def _changed(self):
font = self.currentFont().family()
print(font)
return
myapp = QApplication(sys.argv)
window = Window()
myapp.exec_()
sys.exit()
So I do not want to use a layout for any of the tabs. I will be using .setGeometry to place the objects, as there is many objects to be placed :)
The problem is: Any object created in Tab1.py and Tab2.py are appearing on all the tabs.
Tab3.py code is identical to Tab2.py (just a button)
I believe the issue is with the Parent/Child code, but not sure were I went wrong. Any guidance would be much appreciated. You can also ignore some of the imports, I just haven't gotten to that code yet :)
MainWindow.py
import os, sys, subprocess, atexit, PyQt5, pyodbc, time, datetime, getpass, csv, xlsxwriter
import Tab1, Tab2, Tab3
from PyQt5.QtWidgets import QApplication, QTabBar, QLayout, QLabel, QLineEdit, QMainWindow, QTabWidget, QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QGroupBox, QRadioButton, QTextEdit, QCheckBox, QInputDialog, QFileDialog, QProgressBar, QTableWidget, QTableWidgetItem
from PyQt5.QtCore import *
class MainWindow(QMainWindow):
# define main window size and position on screen
def __init__(MainWindow, parent=None):
super().__init__()
MainWindow.left = 50
MainWindow.top = 50
MainWindow.width = 720
MainWindow.height = 600
MainWindow.setGeometry(MainWindow.left,
MainWindow.top,
MainWindow.width,
MainWindow.height)
MainWindow.table_widget = MainWindowWidget(MainWindow)
MainWindow.setCentralWidget(MainWindow.table_widget)
MainWindowWidget()
class MainWindowWidget(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
# configure layout
self.layout = QVBoxLayout(self)
self.setLayout(self.layout)
# initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.layout.addWidget(self.tabs)
# add tabs
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab2, "Tab3")
# create first tab
Tab1.Tab1.CreateTab1(self)
Tab2.Tab2.CreateTab2(self)
Tab3.Tab3.CreateTab3(self)
def exit_handler(self):
print('closing application')
atexit.register(exit_handler(self))
# create main window and show it
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())
Tab1.py
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMainWindow, QTabWidget, QWidget, QPushButton, QHBoxLayout, \
QVBoxLayout, QGroupBox, QRadioButton, QTextEdit, QCheckBox, QInputDialog, QFileDialog, QProgressBar, QTableWidget, \
QTableWidgetItem, QComboBox
import MainWindow
class Tab1(QMainWindow):
def CreateTab1(tab1, parent=None):
# reference number input box
tab1.label_input_reference_number = QLabel(tab1)
tab1.label_input_reference_number.setText('Reference Number')
tab1.label_input_reference_number.setGeometry(25, 80, 115, 20)
tab1.input_reference_number = QLineEdit(tab1)
tab1.input_reference_number.setGeometry(170, 80, 215, 20)
# add CheckGroupButton
tab1.Button = QPushButton(tab1)
tab1.Button.setText("Button")
tab1.Button.setGeometry(385, 105, 100, 20)
Tab2.py
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMainWindow, QTabWidget, QWidget, QPushButton, QHBoxLayout, QGroupBox, QRadioButton, QTextEdit, QCheckBox, QInputDialog, QFileDialog, QProgressBar, QTableWidget, QTableWidgetItem
class Tab2(QMainWindow):
def CreateTab2(tab2):
# add button
tab2.Button = QPushButton(tab2)
tab2.Button.setText("Another button")
tab2.Button.setGeometry(300, 585, 100, 20)
Your problem is here:
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab2, "Tab3")
# create first tab
Tab1.Tab1.CreateTab1(self)
Tab2.Tab2.CreateTab2(self)
Tab3.Tab3.CreateTab3(self)
You have all of the tab controls being owned by the parent. (Also, you have tab2 in there twice.) You want:
self.tabs.addTab(self.tab1, "Tab1")
self.tabs.addTab(self.tab2, "Tab2")
self.tabs.addTab(self.tab3, "Tab3")
Tab1.Tab1.CreateTab1(tab1)
Tab2.Tab2.CreateTab2(tab2)
Tab3.Tab3.CreateTab3(tab3)
However, this is an odd way to do it. You should have Tab1, Tab2 and Tab3 derive from QWidget, and do this:
self.tab1 = Tab1.Tab1()
self.tab2 = Tab2.Tab2()
self.tab3 = Tab3.Tab3()
That way, you don't need a strange class method to configure things.
Here's why: the tab{i} variable you're passing into each CreateTab{i}(tab{i}) function is the same variable each time; self as referenced in MainWindow. Here's what I'd do; instead of passing self into the call Tab{i}.Tab{i}.CreateTab{i}(self), instead pass in self.tab{i}, the variable you stored earlier. That way separate objects (not the same self i.e. MainWindow) are receiving the separate children widgets.
The header of the next group overwrites the table of the one before. Any ideas on how to protect the table before? I tried to use the spacer but it did not work. Any other ideas how I can solve the problem?
I have a header with e.g. an account number and a name. Below I want to list all the postings. But this program is not only for one account. It is for 100+ accounts to document the balances at a give date. (e.g. at the end of the year)
from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog, QWidget,
QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,
QLabel, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,QScrollArea,
QVBoxLayout,QListWidget,QTableWidget,QTableWidgetItem)
from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal, QSize, QDate
import sys
class Dialog(QDialog):
def __init__(self,parent=None):
super().__init__(parent)
self.setGeometry(64,64, 1000, 400)
self.setWindowTitle("test")
self.scrollarea = QScrollArea(self)
self.scrollarea.setFixedWidth(990)
self.scrollarea.setWidgetResizable(True)
widget = QWidget()
self.scrollarea.setWidget(widget)
self.layout_SArea = QGridLayout(widget)
self.layout_All = QVBoxLayout(self)
self.layout_All.addWidget(self.scrollarea)
for i in range(1,20,2):
self.layout_SArea.addWidget(QLabel("I am the header"),i,0)
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(10)
self.tableWidget.setColumnCount(12)
for x in range(10):
newitem = QTableWidgetItem()
newitem.setData( Qt.EditRole,'{:0,.0f}'.format(x))
newitem.setFlags( Qt.ItemIsSelectable | Qt.ItemIsEnabled )
newitem.setTextAlignment(Qt.AlignRight)
self.tableWidget.setItem(x, 0, newitem)
self.layout_SArea.addWidget(self.tableWidget,i+1,0,i+1,5)
self.layout_All.addLayout(self.layout_SArea)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Dialog()
sys.exit(dialog.exec_())
The row span argument indicates how many "row cells" the in the grid are occupied by the item, so it should not use the row index.
Change to:
self.layout_SArea.addWidget(self.tableWidget, i+1, 0, 1, 5)
I changed the background color to gray and the label color to yellow, but the color of the boxes was also changed to gray and the text inside of them changed color to yellow.
I want to change the colors of the QLineEdit to white and the text inside of them to black, but I don't know how to do it
import sys, re
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QFormLayout, QCheckBox, QComboBox, QSpinBox, QDialogButtonBox, QMessageBox, QErrorMessage, QToolTip, QPushButton, QLineEdit, QLabel, QTextEdit, QMainWindow, QGroupBox, QHBoxLayout, QVBoxLayout
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
import ctypes
class Dialog(QDialog):
NumGridRows = 10
NumButtons = 4
def __init__(self):
super(Dialog, self).__init__()
self.createFormGroupBox()
self.setStyleSheet("background:rgb(66,85,99);")
buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
buttonBox1 = QPushButton('Export',self)
buttonBox1.setStyleSheet("background:rgb(255,199,44);")
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)
buttonBox.setStyleSheet("background:rgb(255,199,44);")
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.formGroupBox)
mainLayout.addWidget(buttonBox)
mainLayout.addWidget(buttonBox1)
self.setLayout(mainLayout)
self.setWindowTitle("Cuadro seguimiento")
def createFormGroupBox(self):
self.formGroupBox = QGroupBox("New")
self.formGroupBox.setStyleSheet("color: rgb(255,199,44);")
layout = QFormLayout()
layout.addRow(QLabel("Fecha"), QLineEdit())
layout.addRow(QLabel("Dias abiertos"), QLineEdit())
layout.addRow(QLabel("Nombre del caso"), QLineEdit())
layout.addRow(QLabel("Responsable"), QLineEdit())
layout.addRow(QLabel("Ok MKT"), QLineEdit())
layout.addRow(QLabel("Solicitud Lotus"), QCheckBox())
layout.addRow(QLabel("OS"), QLineEdit())
layout.addRow(QLabel("Monto"), QLineEdit())
layout.addRow(QLabel("Fecha cierre"), QLineEdit())
layout.addRow(QLabel("Comentario"), QLineEdit())
self.formGroupBox.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Dialog()
sys.exit(dialog.exec_())
I am trying to display an image label with scrollbars within a Box layout.
However, the scroll area appears at the wrong place with the wrong size.
Could you please tell me what I am doing wrong?
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QLabel, QScrollArea
from PyQt5.QtGui import QPixmap
class ApplicationWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
main_widget = QWidget(self)
btn = QPushButton("Bye", self)
btn.clicked.connect(self.close)
img = QPixmap("1.jpg")
label = QLabel(main_widget)
label.setPixmap(img)
scrollArea = QScrollArea(main_widget)
scrollArea.setWidgetResizable(True)
scrollArea.setWidget(label)
l = QVBoxLayout(main_widget)
l.addWidget(label)
l.addWidget(btn)
self.setCentralWidget(main_widget)
def closeEvent(self, ce):
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
aw = ApplicationWindow()
aw.show()
app.exec_()
The result is:
The problem is that instead of adding the QLabel to QVBoxLayout you must add the QScrollArea. You must change:
l.addWidget(label)
to
l.addWidget(scrollArea)