PyQt4 layout delete all QLabels - python

I have a toolbar with right-left controls, and I want to display 2 pages each time the loop ends and then, add them to self.pdf_layout and delete the old ones. The point is to step forwards and backwards 2 pages each time.
How can i replace (or delete and reset) the self.pdf_layout widgets (QLabels- I use them as image placeholders) ???
class pdfViewer(QtGui.QWidget):
def __init__(self, parent,filepath):
QtGui.QWidget.__init__(self, parent)
#temp = tempfile.gettempdir()
#tempfilename=temp+'/unf120.pdf'
self.filepath = filepath
# global CurrentPage
#global currentPage
self.currentPage = 0
self.doc = popplerqt4.Poppler.Document.load(self.filepath)
self.doc.setRenderHint(popplerqt4.Poppler.Document.Antialiasing)
self.doc.setRenderHint(popplerqt4.Poppler.Document.TextAntialiasing)
self.scroll_area = QtGui.QScrollArea()
self.scroll_area.setBackgroundRole(QtGui.QPalette.Dark)
self.mainlayout = QtGui.QVBoxLayout()
#PDF READER CONTROLS
self.ControlsLayout = QtGui.QHBoxLayout()
self.ControlsWidget = QtGui.QWidget()
self.ControlsWidget.setLayout(self.ControlsLayout)
self.pdf_widget = QtGui.QWidget()
self.pdf_layout = QtGui.QHBoxLayout()
self.pdf_widget.setLayout(self.pdf_layout)
#toolBar
self.toolbar = QtGui.QToolBar()
#left Button action
leftAction = QtGui.QAction(QtGui.QIcon('left.png'),'Left',self)
self.toolbar.addAction(leftAction)
#right Button Action
rightAction = QtGui.QAction(QtGui.QIcon('right.png'),'Right',self)
#rightRender = self.renderPages(1,currentPage,100)
self.toolbar.addAction(rightAction)
rightAction.triggered.connect(self.forwardPages)
#start Button Action
startAction = QtGui.QAction(QtGui.QIcon('start.png'),'Go to: Start',self)
self.toolbar.addAction(startAction)
#end Window Action
endAction = QtGui.QAction(QtGui.QIcon('end.png'),'Go to: end',self)
self.toolbar.addAction(endAction)
self.ControlsLayout.addWidget(self.toolbar)
#Zoom Controls
# Zoom Label
zoomLabel = QtGui.QLabel("Zoom: ")
zoomLabel.setMaximumWidth(50)
self.ControlsLayout.addWidget(zoomLabel)
#Zoom ComboBox Widget
combo = QtGui.QComboBox()
combo.addItem("50%")
combo.addItem("60%")
combo.addItem("70%")
combo.addItem("80%")
combo.addItem("90%")
combo.addItem("100%")
combo.addItem("120%")
combo.addItem("150%")
combo.addItem("170%")
combo.addItem("200%")
combo.setMaximumWidth(100)
self.ControlsLayout.addWidget(combo)
# add PDF READER Controls to mainlayout
self.ControlsWidget.setMaximumWidth(300)
self.mainlayout.addWidget(self.ControlsWidget)
# by default goes to firstPage of the current document
self.forwardPages(100)
self.scroll_area.setWidget(self.pdf_widget)
self.mainlayout.addWidget(self.scroll_area)
self.setLayout(self.mainlayout)
self.resize(1700,900)
self.move(500,80)
def firstPage(self,zoomLevel=100):
self.currentPage = 0
pdfpage = self.doc.page(self.currentPage)
image = pdfpage.renderToImage(zoomLevel,zoomLevel)
pixmap = QtGui.QPixmap.fromImage(image)
self.label = QtGui.QLabel()
self.label.setScaledContents(True)
currentWidth = self.scroll_area.frameGeometry().width()-100
#print currentWidth
pixScaled = pixmap.scaledToWidth(currentWidth,QtCore.Qt.FastTransformation)
self.label.setPixmap(pixScaled)
self.pdf_layout.addWidget(self.label)
print 'set first page'
here is the method....
def forwardPages(self,zoomLevel=100):
renderingTime = time.time()
sum_pages = self.doc.numPages()
currentPage = self.currentPage
for currentPage in range(1):
print currentPage
pdfpage = self.doc.page(self.currentPage+1)
image = pdfpage.renderToImage(zoomLevel,zoomLevel)
pixmap = QtGui.QPixmap.fromImage(image)
label = QtGui.QLabel()
label.setScaledContents(True)
currentWidth = self.scroll_area.frameGeometry().width()-100
#print currentWidth
pixScaled = pixmap.scaledToWidth(currentWidth,QtCore.Qt.FastTransformation)
label.setPixmap(pixScaled)
for i in range(self.pdf_layout.count()): self.pdf_layout.itemAt(i).widget().close()
self.pdf_layout.addWidget(label)
print "rendering time = {0}".format(time.time()-renderingTime)
self.scroll_area.setWidget(self.pdf_widget)
self.currentPage = currentPage

You can little bit change your code.
First you store current pages like this
currentLeftPage = self.currentLeftPage
currentRightPage = self.currentRightPage
And you change the pdfpage like
pdfLeftPage = self.doc.page(self.currentLeftPage+2)
pdfRightPage = self.doc.page(self.currentRightPage+2)
then you renderd leftImage and rightImage separatly.
Note: You may modify or optimize the above thing.

Related

PyQt4 - QLineEdit() and QCheckbox()

I am building a GUI for obtaining user inputs and then using them in some complex step later. I have a group of checkboxes from which the user has to choose at least one and also provide an 'alias' name for it in the QLineEdit right below the checkbox (default name is taken otherwise).
Currently, I have to first enter the alias name and then check the checkbox to register the entered name in the line edit to get the value entered by the user and connected checkbox name. This order is not normal.
Is there a way to get the Editline data and the connected checkbox name when 'Continue' is clicked?
Here is my code:
from PyQt4 import QtGui, QtCore
import sys
checkpoint_list = ['Amsterdam','Munich','Paris','Mumbai']
class MyGui(QtGui.QWidget):
def __init__(self):
super(MyGui, self).__init__()
self.initUI()
self.final_list = []
self.platform_list = {}
self.qem = None
def initUI(self):
lay_out = QtGui.QVBoxLayout(self)
# select the CPs
cp_lbl = QtGui.QLabel("Please select CP versions to compare:", self)
lay_out.addWidget(cp_lbl)
self.cb = []
self.platform_label = []
i = 0
for cp in checkpoint_list:
self.cb.append(QtGui.QCheckBox(cp, self))
self.platform_label.append(QtGui.QLineEdit(cp, self))
self.cb[i].stateChanged.connect(self.clickBoxStateChanged)
lay_out.addWidget(self.cb[i])
lay_out.addWidget(self.platform_label[i])
i += 1
lay_out.addStretch(10)
# Continue and cancel button
btn_cancel = QtGui.QPushButton('Cancel', self)
btn_continue = QtGui.QPushButton('Continue', self)
hbox = QtGui.QHBoxLayout()
hbox.addStretch()
hbox.addWidget(btn_continue)
hbox.addWidget(btn_cancel)
vbox = QtGui.QVBoxLayout()
vbox.addStretch()
lay_out.addLayout(hbox)
lay_out.addLayout(vbox)
self.setLayout(lay_out)
btn_cancel.clicked.connect(self.onclick_cancel)
btn_cancel.setToolTip('To <b>Cancel</b> with this process')
btn_continue.clicked.connect(self.onclick_Continue)
btn_continue.setToolTip('To <b>Continue</b> with the matching')
# Screen show
self.setGeometry(300, 300, 500, 400)
self.setWindowTitle('CP Selection Window')
self.show()
def clickBoxStateChanged(self, cb):
self.final_list = []
self.platform_list = {}
for i in range(len(self.cb)):
if self.cb[i].isChecked():
if self.cb[i] not in self.final_list:
self.final_list.append(str(self.cb[i].text()))
self.platform_list[str(self.cb[i].text())] = str(self.platform_label[i].text())
print self.final_list
print self.platform_list
elif self.cb[i].isChecked() == False:
if self.cb[i].text() in self.final_list:
self.final_list.remove(str(self.cb[i].text()))
del self.platform_list[str(self.cb[i].text())]
print self.final_list
print self.platform_list
def onclick_Continue(self):
try:
if len(self.final_list) == 0:
self.qem = QtGui.QErrorMessage(self)
self.qem.showMessage("Please select at least 1 checkpoint to continue...")
else:
self.close()
except:
print "No CP was selected..."
def onclick_cancel(self):
sys.exit()
if __name__ == "__main__":
# GUI code
app = QtGui.QApplication(sys.argv)
w = MyGui()
app.exec_()
The simplest solution is to create a method that analyzes the information and that returns a dictionary of the selected elements:
class MyGui(QtGui.QWidget):
def __init__(self):
super(MyGui, self).__init__()
self.initUI()
def initUI(self):
lay_out = QtGui.QVBoxLayout(self)
# select the CPs
cp_lbl = QtGui.QLabel("Please select CP versions to compare:")
lay_out.addWidget(cp_lbl)
self.cb = []
self.platform_label = []
for cp in checkpoint_list:
cb = QtGui.QCheckBox(cp)
le = QtGui.QLineEdit(cp)
lay_out.addWidget(cb)
lay_out.addWidget(le)
self.cb.append(cb)
self.platform_label.append(le)
lay_out.addStretch(10)
# Continue and cancel button
btn_cancel = QtGui.QPushButton("Cancel")
btn_continue = QtGui.QPushButton("Continue")
hbox = QtGui.QHBoxLayout()
hbox.addStretch()
hbox.addWidget(btn_continue)
hbox.addWidget(btn_cancel)
vbox = QtGui.QVBoxLayout()
vbox.addStretch()
lay_out.addLayout(hbox)
lay_out.addLayout(vbox)
btn_cancel.clicked.connect(self.onclick_cancel)
btn_cancel.setToolTip("To <b>Cancel</b> with this process")
btn_continue.clicked.connect(self.onclick_Continue)
btn_continue.setToolTip("To <b>Continue</b> with the matching")
# Screen show
self.setGeometry(300, 300, 500, 400)
self.setWindowTitle("CP Selection Window")
self.show()
def get_elements_selected(self):
values_selected = dict()
for cb, le in zip(self.cb, self.platform_label):
if cb.isChecked():
values_selected[cb.text()] = le.text()
return values_selected
def onclick_Continue(self):
values = self.get_elements_selected()
if values:
print(values)
self.close()
else:
qem = QtGui.QErrorMessage(self)
qem.showMessage("Please select at least 1 checkpoint to continue...")
qem.exec_()
def onclick_cancel(self):
sys.exit()

Position internal widget inside QStackedWidget object

I have several tabs and inside the "admin" tab I want to display two pages: one locked page (before entering credentials) and another unlocked page (after successful login). To do this, I'm using a QStackedWidget() to switch between the two pages. I have created a locked login screen but can't seem to move the object to the center of the page.
I have looked at moving widgets inside QStackedWidget and centering widgets in the center of the screen but my objects do not seem to change position. I've tried to move the entire internal widget using move() to the center of the screen using the desktop dimension and the parent widget to no avail. How would I be able to move the login fields to the center of the page? Thanks!
Current:
Desired:
Code:
from PyQt4 import QtGui, QtCore
# from load_CSS import load_CSS
# from widgets import UniversalPlotWidget
import sys
import time
def exit_application():
"""Exit program event handler"""
sys.exit(1)
class VerticalTabBar(QtGui.QTabBar):
def __init__(self, width, height, parent=None):
super(VerticalTabBar, self).__init__(parent)
self.width = width
self.height = height
def tabSizeHint(self, index):
return QtCore.QSize(self.width, self.height)
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
tab_options = QtGui.QStyleOptionTab()
for tab in range(self.count()):
self.initStyleOption(tab_options, tab)
painter.drawControl(QtGui.QStyle.CE_TabBarTabShape, tab_options)
painter.save()
size = tab_options.rect.size()
size.transpose()
rectangle = QtCore.QRect(QtCore.QPoint(), size)
rectangle.moveCenter(tab_options.rect.center())
tab_options.rect = rectangle
center = self.tabRect(tab).center()
painter.translate(center)
painter.rotate(90)
painter.translate(-center)
painter.drawControl(QtGui.QStyle.CE_TabBarTabLabel, tab_options);
painter.restore()
class TabWidget(QtGui.QTabWidget):
def __init__(self, *args, **kwargs):
QtGui.QTabWidget.__init__(self, *args, **kwargs)
self.setTabBar(VerticalTabBar(kwargs.pop('width'), kwargs.pop('height')))
self.setTabPosition(QtGui.QTabWidget.West)
self.setTabShape(QtGui.QTabWidget.Rounded)
class AdminTabWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(AdminTabWidget, self).__init__(parent)
self.setWindowModality(QtCore.Qt.ApplicationModal)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.admin_page_locked_init()
self.admin_page_unlocked_init()
self.admin_page_layout = QtGui.QGridLayout()
self.admin_page_switch = QtGui.QStackedWidget()
self.admin_page_switch.addWidget(self.admin_locked_tab)
self.admin_page_switch.addWidget(self.admin_unlocked_tab)
self.admin_page_switch.setCurrentIndex(0)
self.admin_page_layout.addWidget(self.admin_page_switch,0,0)
def admin_page_locked_init(self):
self.admin_locked_tab = QtGui.QWidget()
self.admin_locked_tab.setFixedSize(550,225)
self.admin_locked_layout = QtGui.QGridLayout()
self.username_label = QtGui.QLabel('Username: ')
self.username_field = QtGui.QLineEdit()
self.username_field.returnPressed.connect(self.verify_credentials)
self.space_label = QtGui.QLabel(' ')
self.space_label.setFixedHeight(25)
self.password_label = QtGui.QLabel('Password: ')
self.password_field = QtGui.QLineEdit()
self.password_field.returnPressed.connect(self.verify_credentials)
self.password_field.setEchoMode(QtGui.QLineEdit.Password)
self.verify_button = QtGui.QPushButton('Ok')
self.verify_button.clicked.connect(self.verify_credentials)
self.cancel_button = QtGui.QPushButton('Cancel')
self.cancel_button.clicked.connect(self.unauthorized)
self.status_label = QtGui.QLabel('')
self.status_label.setAlignment(QtCore.Qt.AlignCenter)
self.button_layout = QtGui.QGridLayout()
self.button_layout.addWidget(self.verify_button,0,0,1,1)
self.button_layout.addWidget(self.cancel_button,0,1,1,1)
self.admin_locked_layout.addWidget(self.username_label,0,0,1,1)
self.admin_locked_layout.addWidget(self.username_field,0,1,1,1)
self.admin_locked_layout.addWidget(self.space_label,1,0,1,3)
self.admin_locked_layout.addWidget(self.password_label,2,0,1,1)
self.admin_locked_layout.addWidget(self.password_field,2,1,1,1)
self.admin_locked_layout.addWidget(self.status_label,3,0,1,3)
self.admin_locked_layout.addLayout(self.button_layout,4,0,1,3)
self.admin_locked_tab.setLayout(self.admin_locked_layout)
def verify_credentials(self):
print('button pressed')
# Grab username/password from input fields
self.username = str(self.username_field.text())
self.password = str(self.password_field.text())
self.status_label.setText('Verifying')
self.status_label.setStyleSheet('QLabel {color: rgb(117,255,161)}')
self.spin(.001)
print('verified')
def spin(self, seconds):
"""Pause for set amount of seconds, replaces time.sleep so program doesnt stall"""
time_end = time.time() + seconds
while time.time() < time_end:
QtGui.QApplication.processEvents()
def unauthorized(self):
print('unauthorized')
self.status_label.setText('Invalid username and/or password')
self.status_label.setStyleSheet('QLabel {color: rgb(255,65,106)}')
def admin_page_unlocked_init(self):
self.admin_unlocked_tab = QtGui.QWidget()
admin_unlocked_layout = QtGui.QGridLayout()
admin_unlocked_button = QtGui.QPushButton('unlocked')
admin_unlocked_layout.addWidget(admin_unlocked_button)
self.admin_unlocked_tab.setLayout(admin_unlocked_layout)
def get_admin_page_layout(self):
return self.admin_page_layout
if __name__ == '__main__':
# Create main application window
app = QtGui.QApplication(sys.argv)
# app.setStyleSheet(load_CSS(1))
app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))
font = QtGui.QFont('Ubuntu', 20)
font.setWeight(70)
app.setFont(font)
screen_height = QtGui.QApplication.desktop().screenGeometry().height()
main_window_tab = TabWidget(width=300, height=screen_height/8)
main_window_tab.setWindowTitle("Tab Layout")
main_window_tab.setWindowFlags(QtCore.Qt.FramelessWindowHint)
main_window_tab.showMaximized()
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()
tab3 = QtGui.QWidget()
tab4 = QtGui.QWidget()
tab5 = QtGui.QWidget()
tab6 = QtGui.QWidget()
tab7 = QtGui.QWidget()
admin_tab = QtGui.QWidget()
admin_tab_widget = AdminTabWidget()
admin_tab.setLayout(admin_tab_widget.get_admin_page_layout())
main_window_tab.addTab(admin_tab, "Admin")
main_window_tab.addTab(tab1, "tab1")
main_window_tab.addTab(tab2, "tab2")
main_window_tab.addTab(tab3, "tab3")
main_window_tab.addTab(tab4, "tab4")
main_window_tab.addTab(tab5, "tab5")
main_window_tab.addTab(tab6, "tab6")
main_window_tab.addTab(tab7, "tab7")
main_window_tab.show()
QtGui.QShortcut(QtGui.QKeySequence('Ctrl+Q'), main_window_tab, exit_application)
sys.exit(app.exec_())
The idea is to set the QStackedWidget with the Qt::AlignCenter alignment in the layout so it changes:
self.admin_page_layout.addWidget(self.admin_page_switch, 0, 0)
to:
self.admin_page_layout.addWidget(self.admin_page_switch, 0, 0, alignment=QtCore.Qt.AlignCenter)

Scrolling Text in PyQt?

I'm trying to have text from feedparser scroll across the screen from right to left. I'm using PyQt5, I'm not sure how to go about adding this feature.
What I want to display is below
import feedparser
sports = feedparser.parse('http://rssfeeds.usatoday.com/UsatodaycomSports-TopStories')
for e in sports['entries']:
news = (e.get('title', ''))
I'm looking for a continuous scrolling until all the news headlines are read and then the page is reloaded to get the most recent headlines or just reread whats already there. Thanks!
You can use QTimeLine to show a continously scrolling slice of the news in a label. I implemented it in a little gui to try, if other functions in the app are blocked while QTimeLine is running:
import feedparser
import sys
from PyQt5 import QtWidgets, QtCore
class MyWidget(QtWidgets.QWidget):
def __init__(self, parent = None):
QtWidgets.QWidget.__init__(self, parent)
self.setGeometry(200, 200, 800, 600)
self.textLabel = QtWidgets.QLabel('') # label showing some text
self.uButton = QtWidgets.QPushButton('upper Button')
self.lButton = QtWidgets.QPushButton('lower Button')
self.label = QtWidgets.QLabel('') # label showing the news
self.label.setAlignment(QtCore.Qt.AlignRight) # text starts on the right
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.textLabel)
self.layout.addWidget(self.uButton)
self.layout.addWidget(self.lButton)
self.layout.addWidget(self.label)
self.layout.setStretch(0, 3)
self.layout.setStretch(1, 3)
self.layout.setStretch(2, 3)
self.layout.setStretch(3, 1)
self.setLayout(self.layout)
self.timeLine = QtCore.QTimeLine()
self.timeLine.setCurveShape(QtCore.QTimeLine.LinearCurve) # linear Timeline
self.timeLine.frameChanged.connect(self.setText)
self.timeLine.finished.connect(self.nextNews)
self.signalMapper = QtCore.QSignalMapper(self)
self.signalMapper.mapped[str].connect(self.setTlText)
self.uButton.clicked.connect(self.signalMapper.map)
self.signalMapper.setMapping(self.uButton, self.uButton.text())
self.lButton.clicked.connect(self.signalMapper.map)
self.signalMapper.setMapping(self.lButton, self.lButton.text())
self.feed()
def feed(self):
fm = self.label.fontMetrics()
self.nl = int(self.label.width()/fm.averageCharWidth()) # shown stringlength
news = []
sports = feedparser.parse('http://rssfeeds.usatoday.com/UsatodaycomSports-TopStories')
for e in sports['entries']:
news.append(e.get('title', ''))
appendix = ' '*self.nl # add some spaces at the end
news.append(appendix)
delimiter = ' +++ ' # shown between the messages
self.news = delimiter.join(news)
newsLength = len(self.news) # number of letters in news = frameRange
lps = 4 # letters per second
dur = newsLength*1000/lps # duration until the whole string is shown in milliseconds
self.timeLine.setDuration(dur)
self.timeLine.setFrameRange(0, newsLength)
self.timeLine.start()
def setText(self, number_of_frame):
if number_of_frame < self.nl:
start = 0
else:
start = number_of_frame - self.nl
text = '{}'.format(self.news[start:number_of_frame])
self.label.setText(text)
def nextNews(self):
self.feed() # start again
def setTlText(self, text):
string = '{} pressed'.format(text)
self.textLabel.setText(string)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
Add PySide2 version:
import feedparser
import sys
from PySide2 import QtWidgets, QtCore
class MyWidget(QtWidgets.QWidget):
def __init__(self, parent = None):
QtWidgets.QWidget.__init__(self, parent)
self.setGeometry(200, 200, 800, 600)
self.textLabel = QtWidgets.QLabel('') # label showing some text
self.uButton = QtWidgets.QPushButton('upper Button')
self.lButton = QtWidgets.QPushButton('lower Button')
self.label = QtWidgets.QLabel('') # label showing the news
self.label.setAlignment(QtCore.Qt.AlignRight) # text starts on the right
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.textLabel)
self.layout.addWidget(self.uButton)
self.layout.addWidget(self.lButton)
self.layout.addWidget(self.label)
self.layout.setStretch(0, 3)
self.layout.setStretch(1, 3)
self.layout.setStretch(2, 3)
self.layout.setStretch(3, 1)
self.setLayout(self.layout)
self.timeLine = QtCore.QTimeLine()
self.timeLine.setCurveShape(QtCore.QTimeLine.LinearCurve) # linear Timeline
self.timeLine.frameChanged.connect(self.setText)
self.timeLine.finished.connect(self.nextNews)
self.signalMapper = QtCore.QSignalMapper(self)
self.signalMapper.mapped[str].connect(self.setTlText)
self.uButton.clicked.connect(self.signalMapper.map)
self.signalMapper.setMapping(self.uButton, self.uButton.text())
self.lButton.clicked.connect(self.signalMapper.map)
self.signalMapper.setMapping(self.lButton, self.lButton.text())
self.feed()
def feed(self):
fm = self.label.fontMetrics()
self.nl = int(self.label.width()/fm.averageCharWidth()) # shown stringlength
news = []
sports = feedparser.parse('http://rssfeeds.usatoday.com/UsatodaycomSports-TopStories')
for e in sports['entries']:
news.append(e.get('title', ''))
appendix = ' '*self.nl # add some spaces at the end
news.append(appendix)
delimiter = ' +++ ' # shown between the messages
self.news = delimiter.join(news)
newsLength = len(self.news) # number of letters in news = frameRange
lps = 4 # letters per second
dur = newsLength*1000/lps # duration until the whole string is shown in milliseconds
self.timeLine.setDuration(dur)
self.timeLine.setFrameRange(0, newsLength)
self.timeLine.start()
def setText(self, number_of_frame):
if number_of_frame < self.nl:
start = 0
else:
start = number_of_frame - self.nl
text = '{}'.format(self.news[start:number_of_frame])
self.label.setText(text)
def nextNews(self):
self.feed() # start again
def setTlText(self, text):
string = '{} pressed'.format(text)
self.textLabel.setText(string)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())

Python PyQt QScrollArea and QGridLayout (or any?)

I am trying to generate a Dialog with a stacked layout of GridLayout (of CheckBox's) and HLayout. Well, I did. But my grid got big (my HLayout is fine) but now I want a ScrollArea so as to not take up too much real estate. I have tried the following code .... and it generates a scroll view, but the size of the scroll view is the size of a button (the first ofc) in the HLayout. I want the scroll view to be the width of the Dialog, which nominally would be the minimum width of the HLayout. Additionally adding the scroll_view and scroll_viewWidget cause a QLayout exception to be raised, QLayout::addChildLayout: layout "" already has a parent. Any idea's?
class checkboxDialog(QtGui.QDialog) :
def __init__(self, headers, name) :
super(checkboxDialog, self).__init__()
self.checkerLayout = QtGui.QVBoxLayout()
self.checkerLayout.setMargin(1)
self.checkerLayout.setSpacing(2)
self.scroll_view = QtGui.QScrollArea(self)
self.scroll_view.setWidgetResizable(True)
self.scroll_viewWidget = QtGui.QWidget()
self.scroll_viewWidget.setGeometry(QtCore.QRect(0, 0, 600, 400))
self.scroll_view.setWidget(self.scroll_viewWidget)
self.checkerHlayout = QtGui.QHBoxLayout(self.scroll_viewWidget)
checksLayout = QtGui.QGridLayout()
checksLayout.setColumnStretch(90,7)
checksLayout.setMargin(1)
checksLayout.setSpacing(2)
self.cbList = []
index = 0
for row_index in range(90):
for column_index in range(7):
if index > len(headers)-1 : break
checkbox = QtGui.QCheckBox(headers[index][0:8], self)
checkbox.setToolTip("Chanel = {}".format(headers[index]))
self.cbList.append(checkbox)
# Hide the Phase channels for now ... not sure I shoudl even build the CheckBoxes
# But if I dont then the len(cbList) < len(data[0])
if headers[index][-1] == 'p' :
checkbox.hide()
checksLayout.addWidget(checkbox, row_index, column_index)
index += 1
self.checkerHlayout.addLayout(checksLayout)
self.buttonLayout = QtGui.QHBoxLayout()
self.buttonLayout.setMargin(1)
self.buttonLayout.setSpacing(2)
applyButton = QtGui.QPushButton("Apply")
nextButton = QtGui.QPushButton("Next")
nextAllButton = QtGui.QPushButton("NextAll")
prevButton = QtGui.QPushButton("Prev")
prevAllButton = QtGui.QPushButton("PrevAll")
clearButton = QtGui.QPushButton("Clear")
self.buttonLayout.addWidget(applyButton)
self.buttonLayout.addWidget(nextButton)
self.buttonLayout.addWidget(nextAllButton)
self.buttonLayout.addWidget(prevButton)
self.buttonLayout.addWidget(prevAllButton)
self.buttonLayout.addWidget(clearButton)
self.checkerLayout.addLayout(self.checkerHlayout)
self.checkerLayout.addLayout(self.buttonLayout)
self.setLayout(self.checkerLayout)
self.setObjectName(name)
self.setWindowTitle(name)
self.connect(applyButton, QtCore.SIGNAL('clicked()'), self.checked)
self.connect(nextButton, QtCore.SIGNAL('clicked()'), self.next_chn)
self.connect(nextAllButton, QtCore.SIGNAL('clicked()'), self.next_chnAll)
self.connect(prevButton, QtCore.SIGNAL('clicked()'), self.prev_chn)
self.connect(prevAllButton, QtCore.SIGNAL('clicked()'), self.prev_chnAll)
self.connect(clearButton, QtCore.SIGNAL('clicked()'), self.clear_chn)
I think this is what you wanted:
self.w2 = QtGui.QWidget(self)
self.w2.setLayout(self.buttonLayout)
self.checkerLayout.addWidget(self.w2)
self.checkerLayout.addWidget(self.scroll_view)
self.setLayout(self.checkerLayout)
(replacing the block with addLayout statements)

Removing thin borders between widgets in Qt

In particular I have 2 questions:
1) How can I remove those thin lines between the widgets? setMargin(0) and setSpacing(0)
are already set.
2) In a further step I want to remove the window title bar with FramelessWindowHint.
To drag the window, I'll bind a mouseevent on the upper dark yellow widget. Right now, the upper widget is a QTextEdit with suppressed keyboard interactions. For the draging purpose, I doubt this widget is good... So the question is, what other widgets are good to create a colored handle to drag the window? Perhaps QLabel?
EDIT: Here is the code. I only used QTestEdit-Widgets.
from PyQt4.QtGui import *
from PyQt4 import QtGui,QtCore
import sys
class Note(QWidget):
def __init__(self, parent = None):
super(QWidget, self).__init__(parent)
self.createLayout()
self.setWindowTitle("Note")
def createLayout(self):
textedit = QTextEdit()
grip = QTextEdit()
grip.setMaximumHeight(16) #reduces the upper text widget to a height to look like a grip of a note
grip.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) #suppresses the scroll bar that appears under a certain height
empty = QTextEdit()
empty.setMaximumHeight(16)
empty.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
resize = QTextEdit()
resize.setMaximumHeight(16)
resize.setMaximumWidth(16)
resize.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
layout = QVBoxLayout()
layout.addWidget(grip)
layout.addWidget(textedit)
layout.setMargin(0)
layout.setSpacing(0)
layoutBottom=QHBoxLayout()
layoutBottom.addWidget(empty)
layoutBottom.addWidget(resize)
layout.addLayout(layoutBottom)
self.setLayout(layout)
# Set Font
textedit.setFont(QFont("Arial",16))
# Set Color
pal=QtGui.QPalette()
rgb=QtGui.QColor(232,223,80) #Textwidget BG = yellow
pal.setColor(QtGui.QPalette.Base,rgb)
textc=QtGui.QColor(0,0,0)
pal.setColor(QtGui.QPalette.Text,textc)
textedit.setPalette(pal)
empty.setPalette(pal)
pal_grip=QtGui.QPalette()
rgb_grip = QtGui.QColor(217,207,45)
pal_grip.setColor(QtGui.QPalette.Base,rgb_grip)
textc_grip=QtGui.QColor(0,0,0)
pal.setColor(QtGui.QPalette.Text,textc_grip)
grip.setPalette(pal_grip)
resize.setPalette(pal_grip)
resize.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
empty.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
grip.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
#textedit.setTextInteractionFlags(QtCore.Qt.NoTextInteraction) #total text widget lock
#textedit.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse) #Lock?
#http://qt-project.org/doc/qt-4.8/qt.html#TextInteractionFlag-enum
#self.setWindowFlags(QtCore.Qt.FramelessWindowHint) #removes the title bar
#self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) #to make the window stay on top
class Main():
def __init__(self):
self.notes=[]
self.app = QApplication(sys.argv)
self.app.setQuitOnLastWindowClosed(False);
self.trayIcon = QSystemTrayIcon(QIcon(r"C:\Users\Thomas\Desktop\SimpleNotes.ico"), self.app)
self.menu = QMenu()
self.newWindow = self.menu.addAction("New note")
self.separator = self.menu.addSeparator()
self.hideNotes = self.menu.addAction("Hide all notes")
self.showNotes = self.menu.addAction("Show all notes")
self.separator = self.menu.addSeparator()
self.saveNotes = self.menu.addAction("Save notes")
self.loadNotes = self.menu.addAction("Load notes")
self.separator = self.menu.addSeparator()
self.showHelp = self.menu.addAction("Show help")
self.showAbout = self.menu.addAction("Show about")
self.separator = self.menu.addSeparator()
self.exitAction = self.menu.addAction("Quit notes")
self.exitAction.triggered.connect(self.close)
self.newWindow.triggered.connect(self.newNote)
self.trayIcon.setContextMenu(self.menu)
self.trayIcon.show()
self.app.exec()
def newNote(self):
print("Create new note entry has been clicked")
note=Note()
note.show()
self.notes.append(note)
print(self.notes)
def hideNotes(self):
pass
def showNotes(self):
pass
def saveNotes(self):
pass
def loadNotes(self):
pass
def showHelp(self):
pass
def showAbout(self):
pass
def close(self):
self.trayIcon.hide()
self.app.exit()
print("Exit menu entry has been clicked")
if __name__ == '__main__':
Main()
The answer from thuga was good enough, so i post it here:
textedit.setFrameShape(QtGui.QFrame.NoFrame)
and
grip.setFrameShape(QtGui.QFrame.NoFrame)
made the line disappear.
for 1. I've used:
textEdit.setFrameStyle(QtGui.QFrame.NoFrame)
ui->fr200->setFrameShape(QFrame::NoFrame);
ui->fr201->setFrameShape(QFrame::NoFrame);
ui->fr202->setFrameShape(QFrame::NoFrame);
ui->fr203->setFrameShape(QFrame::NoFrame);
ui->fr204->setFrameShape(QFrame::NoFrame);
ui->fr205->setFrameShape(QFrame::NoFrame);
ui->fr206->setFrameShape(QFrame::NoFrame);
ui->fr207->setFrameShape(QFrame::NoFrame);
ui->fr208->setFrameShape(QFrame::NoFrame);

Categories