QDockWidget, place the tabs on top and insert text - python

Are there any methods to make the tabs be placed on top? I think it's more suitable. And is there an easy way to name the tabs, maybe add a QLabel.
Below is a picture of how it looks now.

Use setTabPosition to put the tabs at the top for the relevant dock-areas:
mainwindow.setTabPosition(QtCore.Qt.AllDockWidgetAreas, QtGui.QTabWidget.North)
The tab text is taken from the window title, so it can be set like this:
dockwidget.setWindowTitle('Name')
or indirectly via the QDockWidget constructor:
dockwidget = QtGui.QDockWidget('Name', parent)

Related

Styling problem. Cannot access specific button

I want to add images to my buttons. I have all my styling in a separate file.
According to Qt docs https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types I should be able to access a specific button using the following syntax
QPushButton#my_button_name {}
but it does not seem to work.
Here is an example from my code:
# script with widgets and layouts
class Tab1():
self.button1 = QPushButton()
# styling script
def button_style():
return '''QPushButton#button1 {font:15px;}'''
I am misunderstanding something? I tried adding 'self.' to the name in the styling script but it does not work either.
Name selectors for stylesheets must be used by setting the objectName property; python attribute naming is completely useless for this as Qt doesn't know anything about it.
In order to correctly apply the stylesheet as you want to, you need to set the object name:
# ...
self.button1 = QPushButton()
self.button1.setObjectName('button1')
self.setStyleSheet('QPushButton#button1 {font:15px;}')

pyqt: how to use scroll area that contains an outsized label [duplicate]

I want to overlay two widgets in QtDesigner:
There is the big QTextBrowser, and below in the down right corner should be a non-interactiv label that I am going to use as a drag-grip to resize the window (the main widget is frameless so I need to implement it).
Usually this label will sit below the QTextBrowser, which leaves on the left of the grip-label a lot of unused space. So I want to put the grip-label above the QTextBrowser. I want to achieve this in QtDesigner. But the code would look like:
QHBoxLayout *layout = new QHBoxLayout(videoWidget);
QLabel *overlayWidget = new QLabel();
overlay->setAlignment(Qt::AlignCenter);
overlay->setText("Overlaid Text");
layout->addWidget(overlay);
Or as I already did in python:
self.textedit = QTextBrowser(self);
...
gripImage=QLabel(self.textedit);
There the part between the brackets is the parent widget.
That's how it looks right now, but this is not what I want:
This is usually simplest to achieve by using QGridLayout. It allows widgets to occupy the same grid cells. For this particular problem, a 1x1 grid is enough.
Steps to try it out with designer:
Create new form, plain Widget for simplicity
Add a text edit to it (drag and drop from Widget Box), and from Object Inspector you should see it becomes child of the root widget
Add a label to it (drag and drop from Widget Box), and from Object Inspector you should see it becomes child of the root widget
Right click on the root widget (easiest in the Object Inspector), and from the bottom of context menu, select Lay out > - Lay out in Grid
Right click on the label, and from Layout alignment > set it aligned to the corner you want
Done. Here's what it looks like in my Designer:
Now adapt above to your real form.
Ok, it appears achieving above with Designer is hard, and perhaps a bit a matter of luck of doing things just right... Designer just doesn't support doing what you want, it seems.
For clarity this is a complete source code:
QGridLayout *layout = new QGridLayout(widget);
QTextBrowser *textBrowser = new QTextBrowser();
QLabel *label = new QLabel();
label->setText("Overlaid Text");
//label gets positioned above textBrowser and is an overlay
layout->addWidget(textBrowser, 0, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(label, 0, 0, Qt::AlignRight | Qt::AlignBottom);
I had the same problem but I did not manage to add Overlapping widgets in QtDesigner. Instead, I had to create the overlapping one dynamically after initializing my MainWindow.
I've got two widgets:
dataset_tableWidget (tableWidget)
spinner_dataset_tableWidget (QtWaitingSpinner)
and I wanted to make spinner_dataset_tableWidget spin over the dataset_tableWidget.
After initializing the MainWindow you can do:
#Crating QtWaitingSpinners dinamically and positioning it over the tableWidgets
dataset_tableWidget = QtWaitingSpinner(dataset_tableWidget)
dataset_tableWidget.setSizePolicy(dataset_tableWidget.sizePolicy())

cursor gone in PyQT

I have a window containing multiple QRowWidgets, which are custom widgets defined by me. These QRowWidgets contain QLineEdits and other standard widgets. To show or hide certain parts of a QRowWidget, I overdefined the focusInEvent() methodes of all the widgets within it. It works perfectly, when I click on the QRowWidget, the hidden elements appear.
The weird thing is that the blinking cursor line hovewer doesn't appear in the QLineEdits within the custom widgets. I can select them both by a mouse click or with Tab, and a glow effect indicates that the QLineEdit is selected in it, I can select a text in it, or start typing at any location wherever I clicked, but the cursor never appears and it's quite annoying.
My 1st thought was that it is a bug on Mac, but I have the same experience on SuSe Linux.
I'm using python 2.7 and PyQt4.
This is in the __init__() of the QRowWidget:
for i in self.findChildren(QWidget):
i.focusInEvent = self.focusInEvent
And then this is the own focusInEvent():
def focusInEvent(self, event):
if self.pself.focusedLine:
self.pself.focusedLine.setStyleSheet("color: #666;")
self.pself.focusedLine.desc.hide()
self.pself.focusedLine.closebutton.hide()
self.setStyleSheet("color: #000;")
self.desc.show()
self.closebutton.show()
self.pself.focusedLine = self
I suspect you do not make a call to the original focusInEvent() when you override it. Your function should look something like:
def focusInEvent(self,...):
QParent.focusInEvent(self,...)
# the rest of your code
where QParent is the nearest base class for your widgets is.
Either that, or make sure you call focusInEvent() on your QLineEdit widgets as part of your function.
Given the comments, it sounds like you are dynamically reassigning the focusInEvent function on the insantiatations in your custom widget. I would either make a derived class for each of the widgets you use that just overrides focusInEvent as above, or include a line like
type(self).focusInEvent(self,..)
in you function.

Layout inside QtGui.QTabWidget PySide

How can I add a layout but not a widget inside a QtGui.QTabWidget? Something like this:
tabs = QtGui.QTabWidget()
tabs.addTab(QtGui.QVBoxLayout(), "Layout")
You don't add layouts to a tab widget, you add widgets.
The simplest choice here would be a QFrame (or even a plain QWidget), to which you can set whatever layout you want.

wx Python Label Right Align

I have a label that I want right aligned and the text to be right aligned. But when my code runs through and the label updates it, the StaticText aligns to the left of a button object. My code is below
hbox14 = wx.BoxSizer(wx.HORIZONTAL)
self.buttonRemove = wx.Button(self.panel,label='Remove')
self.buttonRemove.Bind(wx.EVT_BUTTON,self.removeAccount) # Remove account from list
self.labelSecTic = wx.StaticText(self.panel,label='0.0',style=wx.TE_RIGHT|wx.EXPAND)
self.labelSecTic.SetForegroundColour('white')
self.labelSecTic.SetBackgroundColour('black')
hbox14.Add(self.buttonRemove,proportion=0)
hbox14.Add(self.labelSecTic,proportion=1,flag=wx.ALIGN_RIGHT|wx.TE_RIGHT|wx.EXPAND)
When the label is updated I call
self.gui.labelSecTic.SetLabel(str(self.diff))
Any suggestions on how to make the labelSecTic stay fixed to the right side of the panel? Thanks.
First a side note: the style wx.TE_RIGHT is for wx.TextCtrl, it probably does nothing with the static text. About your real issue, you should force layout of the hbox14 sizer. Not sure what is the sizer/panel structure of your window, you should call Layout on some ancestor of hbox14, it might be self.gui.panel or even self.gui (don't know what gui is), so for example:
self.gui.labelSecTic.SetLabel(str(self.diff))
self.gui.Layout()
or
self.gui.labelSecTic.SetLabel(str(self.diff))
self.gui.panel.Layout()

Categories

Resources