How to set QToolButton fill in the side bar(QDockWidget) in Qt? - c++

I wrote a minimal example which has a sidebar which contains a QToolButton on it. I set setAutoRaise(true) for the QToolButton, so when hover on it, the button will raise. But currently I have a minor issue. As you can see from the picture below, when hover on the button, the border on the right and left are not fully take the whole screen.
Here is how that looks like:
And this example what I want to button to look like:
And here is my code:
sidebarDock = new QDockWidget(this);
addDockWidget(Qt::LeftDockWidgetArea, sidebarDock);
//hide dock widget title bar
QWidget *titleBarWidget = new QWidget(sidebarDock);
sidebarDock->setTitleBarWidget(titleBarWidget);
sidebarDock->titleBarWidget()->hide();
dockWidget = new QWidget(sidebarDock);
dockWidget->setObjectName("DockWidget");
dockWidget->setStyleSheet("#DockWidget { background-color: #F7DC6F; }");
dockVLayout = new QVBoxLayout(dockWidget);
overviewBtn = new QToolButton(dockWidget);
overviewBtn->setAutoRaise(true);
overviewBtn->setIcon(QIcon(":/Icons/overview.png"));
overviewBtn->setText("Overview");
overviewBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
dockVLayout->addWidget(overviewBtn);
dockWidget->setLayout(dockVLayout);
sidebarDock->setWidget(dockWidget);
So can someone tell me which part I missed to set the QQToolButton right and left border completely to side? Or are there some better ways to achieve this? Thanks.

Now I solved this problem.
Just need to add one line to the code snippet to set the layout's margin to 0, using: dockVLayout->setMargin(0)

Related

How to remove focus from MyGUI Widget?

I have a MyGUI::ButtonPtr and on click of this widget I am showing a QWidget. By default focus is on QWdiget but it seems that MyGUI widget also has focus which is creating few issues for me.
myButton = widPtr.at(0)->findWidget("settings")->castType<MyGUI::Button>();
myButton->eventMouseButtonClick += MyGUI::newDelegate(this, settingsClicked);
addToolTip(myButton, "Tooltip text");
void addToolTip(MyGUI::Widget *widget, QString toolTipLabel)
{
widget->eventToolTip += MyGUI::newDelegate(this, notifyTooltipEvent);
widget->setNeedToolTip(true);
widget->setUserString("tooltip", toolTipLabel.toStdString());
}
This tooltip should only be displayed on mouseover but it is visible also when button is clicked and QWidget is open which is incorrect. Reason for this seems to me that MyGUI button still has focus due to which tooltip is being displayed. I wish to remove this focus from MyGUI button.
This worked for me.
MyGUI::InputManager::getInstancePtr()->injectMouseRelease(0, 0, MyGUI::MouseButton::Button0);

QGraphicsOpacityEffect on QTextField on Windows 7 -- strange behavior

I am attempting to add a fade-in/fade-out effect to a widget (that contains other widgets, including buttons and text fields).
The problem is that while buttons fade perfectly, text fields do not - instead of changing opacity of the text field, only the opacity of its border changes.If the text has been selected before changing opacity, after the opacity is reduced, the selection looks strange.
What can be the cause of this problem and how to make the fade effect on all widgets, not only push buttons?
The problem can be easily reproduced, if you place a QTextField on form, and add the following code to button handlers:
void MainWindow::on_pushButton_3_clicked()
{
QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect(ui->plainTextEdit);
effect->setOpacity(0.1);
ui->plainTextEdit->setGraphicsEffect(effect);
}
After this code is run, you will see something like this:
Image1
void MainWindow::on_pushButton_4_clicked()
{
QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect(ui->plainTextEdit);
effect->setOpacity(1.0);
ui->plainTextEdit->setGraphicsEffect(effect);
}
The text field looks like this, after this method is executed:
Image2
Thanks.

Making QMessageBox InformativeText Bold and increase font size

In my qt C++ gui application, I have a QMessageBox with 3 buttons and some standard functionality. I want to decorate all these with stylesheets and fonts and what not.
I am successfully able to do the following -
i. Set MessageBox background.
ii. Set Button background.
void MainWindow::on_pushButton_clicked()
{
QMessageBox msgbox;
msgbox.setInformativeText("YOLO");
msgbox.setStandardButtons(QMessageBox::Ok|QMessageBox::No|QMessageBox::Cancel);
msgbox.setWindowFlags(Qt::FramelessWindowHint);
msgbox.setStyleSheet("QMessageBox { background-image: url(:/res/bk.jpg) }");
msgbox.button(QMessageBox::Ok)->setStyleSheet("QPushButton { background-image: url(:/res/green_bk.jpg) }");
msgbox.button(QMessageBox::No)->setStyleSheet("QPushButton { background-image: url(:/res/red_bk.jpeg) }");
msgbox.exec();
}
But I am stuck on how to set a font (type and size) to the InformativeText field, and also how to make it Bold. I have referred to the InformativeText Properties but I am unable to sort things out. Any ideas on how to achieve this ? Thanks.
As an added query, can I change the fonts (type and size) of buttons as well and set Bold Italics etc ?
EDIT
I did the following changes to my code, and the results are baffling,
QFont font;
font.setBold(true);
msgbox.setFont(font);
After doing this, my informationText has become bold, my cancel button has become bold. But the Ok and No buttons are normal (non-bold). If I remove the setStylesheets for the two buttons, only then are they becoming bold (which is useless, since I want background images).
Any ideas to have a synergy in all these ???
I found solution for your problems.
QMessageBox msg(QMessageBox::Information,"Hey", "Dude", QMessageBox::Ok);
QFont font;
font.setBold(true);
msg.setFont(font);
msg.button(QMessageBox::Ok)->setFont(font);
msg.exec();

Internal QWidgets of QTabBar's tab?

As a follow up of " Hide label text for Qt tabs without setting text to empty string " :
Can I directly access the widgets within the tabs of the QTabBar. I do not mean the corresponding widget which is shown when I select a tab, but the tab's widgets (so in the screenshot below the log label and log icon).
I have tried QTabBar::findChildren, but with no success. Any idea?
QTabBar header sections are not actually widgets. They are drawn by QStylePainter inside QTabBar::paintEvent. Thus you can't get access to them.
As a workaround you can add a tab with an empty text and set a custom widget to it:
QTabBar *bar = new QTabBar;
bar->addTab("");
QLabel *label = new QLabel("my label");
bar->setTabButton(0, QTabBar::LeftSide, label);

Removing borders from QWidgets in a QGraphicsGridLayout

I currently have a QGraphicsScene that I am using with a QGraphicsGridLayout. I am trying to align QWidgets (QLabels and a custom graph QWidget) on this grid layout, and then export it to a QPrinter for pdf export.
The problem I'm having is that I have these grey divider lines between the QLabels that I can't seem to get rid of. I have tried settings spacing in the layout to 0, margins to 0, all the different properties of the QLabel palette, etc. all to no avail. Here's the relevant code:
main class:
QLabel lbl("some text");
lbl.setAutoFillBackground(true);
QPalette pal = lbl.palette();
pal.setColor(QPalette::Window, Qt::white);
lbl.setPalette(pal);
lbl.setFrameStyle(QFrame::NoFrame);
reportlayout->addWidget(&lbl);
reportlayout->generatePDF(reportfilename);
reportlayout class:
gridlayout->setContentsMargins(0,0,0,0);
gridlayout->setSpacing(0);
QGraphicsWidget* page = new QGraphicsWidget();
page->setLayout(gridlayout);
scene->addItem(page);
printer->setOutputFileName(filename);
painter->begin(printer);
scene->render(painter);
painter->end();
I have a feeling that it is the layout doing this, as the lines are between cells in the grid - but the layout doesn't have any color properties and I couldn't find anything to do with divider lines.
Thanks a bunch!
Have you tried stylesheets?
For example,
setStylesheet("QLabel { border:0px solid black; }");
You must investigate all possible selectors till find which one introduces the border.