drop shadow effect on QPushButton text - c++

How do I set a drop-shadow effect on a QPushButton text?
I could set shadow on the entire QPushButton using QGraphicsDropShadowEffect, I however, am not able to find a way to directly set the effect of text inside the QButton.
Edit:
I tried the following, not sure if the syntax is correct
in the .qss file:
MyButton::text
{
shadow: palette(dark);
}
I set the button's drop shadow effect by:
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect( );
effect->setBlurRadius( 5 );
mStartButton->setGraphicsEffect( effect );

Try this:
Set a QLabel iside QPushButton rather than simple text. Then apply shadow effect to the label.
You may need to add extra code for centering the label inside the pushbutton.
mStartButton->setText("");
QLabel *label = new QLabel(mStartButton);
label->setText("<b>Button</b>");
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect( );
effect->setBlurRadius( 5 );
label ->setGraphicsEffect( effect );

Related

Issue with QLayout with QStackedWidget

I am implementing a tab style UI. where tabs are shown by QListWidget and contents are shown by QStackedWidget. on every page of QStackWidget there is layout which allow to insert panel(widget) in QHBoxlayout. at every panel, there are couple of icons which are again in QHBoxLayout. Below is ideal case which I wanted to implement.
But on other page of QStackWidget this is not the case (with less icons) as below
I want to remove extra space ( or align icon to left to eliminate extra space among icons on panels)
I tried spacer, then this happened :(
please help me to correct this thing. My spacer code is as
inline QSpacerItem * buildSpacer(Qt::Orientation orientation)
{
QSpacerItem * pSpacer = nullptr;
if (orientation == Qt::Horizontal)
{
pSpacer = new QSpacerItem(1000, UNIT_VALUE, QSizePolicy::Expanding, QSizePolicy::Minimum);
}
else
{
pSpacer = new QSpacerItem(UNIT_VALUE, 1000, QSizePolicy::Minimum, QSizePolicy::Expanding);
}
return pSpacer;
}
Note
I donot want to use QTabWidget. By the way this issue is also with QTabWidget
Why not using QTabWidget in the first place?
Anyway, instead of creating the QSpacerItem by yourself, you should use addStretch():
my_layout->addWidget(new Widget("widget1"));
my_layout->addWidget(new Widget("widget2"));
my_layout->addStretch(1); // will "eat" extra space

Qt adding drop shadow effect for QLabel using QGraphicsDropShadow

I have to apply drop shadow for multiple QLabel in my Application. I used QGraphicsDropShadowEffect and it is working fine, if i am adding it for one QLabel. I tried applying the same Graphics effect for two QLabels.
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setColor(Qt::white);
effect->setBlurRadius(0);
effect->setXOffset(1);
effect->setYOffset(0);
QLabel* label = new QLabel();
label->setText("QLabel1");
label->setGraphicsEffect(effect);
QLabel* label2 = new QLabel();
label2->setText("QLabel2");
label2->setGraphicsEffect(effect);
In this case,the shadow effect is applied only to label2.
I tried creating two different QGraphicsDropShadowEffect objects and setting the QLabels with that.
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setColor(Qt::white);
QLabel* label = new QLabel();
label->setText("QLabel1");
label->setGraphicsEffect(effect);
QGraphicsDropShadowEffect* effect1 = new QGraphicsDropShadowEffect();
effect1->setColor(Qt::white);
QLabel* label2 = new QLabel();
label2->setText("QLabel2");
label2->setGraphicsEffect(effect1);
In this case, Application is crashing in QRasterPaintEngine::transformChanged() call.
Any idea on how to fix this issue?
I am using qt 5.3.
Was just trying to solve this myself. As much as this solution pains me it's the best I came up with quickly. Use QList to help yourself out here:
// List instances containing labels and drop shadows
QList<QLabel*> label_list_;
QList<QGraphicsDropShadowEffect*> shadow_list_;
// Get all UI labels and apply shadows
label_list_ = this->findChildren<QLabel*>();
foreach(QLabel *lbl, label_list_) {
shadow_list_.append(new QGraphicsDropShadowEffect);
shadow_list_.back()->setBlurRadius(10);
shadow_list_.back()->setOffset(3, 3);
lbl->setGraphicsEffect(shadow_list_.back());
}
If don't want all UI labels in your list you can add manually with append

Qt: How to add two widgets (say QPushButton) to the status bar, one to the left and other to the right side?

I would like to add two widgets (say QPushButton) to the status bar, one to the left and other to the right side.
I am thinking of adding horizontal spacer in between the two widgets, but don't know how to add.
PS: I tried using addWidget() to add to the left and addPermanentWidget() to add to the right but it doesn't look neat and also it doesn't feel right.
You can add two buttons to a layout in a widget and add the widget to the status bar using QStatusBar::addWidget :
QWidget * widget = new QWidget();
QPushButton * leftBut = new QPushButton("Left");
QPushButton * rightBut = new QPushButton("Right");
QGridLayout * layout = new QGridLayout(widget);
layout->addWidget(leftBut,0,0,1,1,Qt::AlignVCenter | Qt::AlignLeft);
layout->addWidget(rightBut,0,1,1,1,Qt::AlignVCenter | Qt::AlignRight);
ui->statusBar->addWidget(widget,1);
I am thinking of adding horizontal spacer in between the two widgets, but don't know how to add.
Here is a way to use a "fake" spacer.
QPushButton *leftButton = new QPushButton("Left");
QPushButton *rightButton = new QPushButton("Right");
QLabel *spacer = new QLabel(); // fake spacer
ui->statusBar->addPermanentWidget(leftButton);
ui->statusBar->addPermanentWidget(spacer, 1);
ui->statusBar->addPermanentWidget(rightButton);
The second parameter in addPermanentWidget is "used to compute a suitable size for the given widget as the status bar grows and shrinks".
Demo:
I think the simplest way is using a QGridLayout (honestly I never tried to modify a status bar anyway) supposing that the status bar is or descends from widget you can do this:
QGridLayout *myGridLayout = new QGridLayout();
statusbar->setLayout(myGridLayout)
QPushButton *button1 = new QPushButton(this);
myGridLayout->addWidget(button1,0,0,1,1);
QPushButton *button2 = new QPushButton(this);
myGridLayout->addWidget(button2,X,0,1,1);
The biggest is X the more space you want to leave in between, I would suggest to start with 3 and then make few tests to see how it looks.

Qt - Get QPushButton icon name

I have a two state QPushButton. I want to associate an icon to each state.
It is like Play|Pause buttons in music players.
To do so, I would like to get the current icon name in order to know what the next icon to set will be.
I could subclass QPushButton but is it worth it?
Instead of setting an icon based on the QPushButton's state, set one QIcon that has two states, Qt will select the correct icon if you use it with a checkable QPushButton.
QIcon icon = QIcon();
// 'Off' state corresponds to unchecked state of QPushButton
icon.addPixmap( QPixmap( ":/img/play.png" ), QIcon::Normal, QIcon::Off );
// 'On' state corresponds to checked state of QPushButton
icon.addPixmap( QPixmap( ":/img/pause.png" ), QIcon::Normal, QIcon::On );
QPushButton * button = new QPushButton();
button->setIcon( icon );
button->setCheckable( true );
Use QPushButton::icon() and QIcon::name() to get the icon name.

How do I add a QTCreator-like left bar to my program?

I'm designing the GUI for a project, and I want a left bar like this ones
(source: patatux.net)
(source: tuxradar.com)
How do I put them in my .ui file?
You can try to use QToolBar with vertical orientation.
To emulate tabs behavior you should put actions to QActionGroup and make them checkable.
For example to create left panel Qt creator like:
welcomeAct = new QAction(...)
toolbar->addAction(welcomeAct)
editAct = new QAction(...)
toolbar->addAction(editAct)
designAct = new QAction(...)
toolbar->addAction(designAct)
...
//add spacing
QLabel *spacing = new QLabel;
spacing->setSizePolicy(Qt::Expanding, Qt::Expanding);
toolbar->addWidget(spacing);
//adding aditional actions
runAct = new QAction(...)
toolbar->addAction(runAct)
runDebugAct = new QAction(...)
toolbar->addAction(runDebugAct)
buildAct = new QAction(...)
toolbar->addAction(buildAct)
// put "tabs" action in QActionGroup
group = new QActionGroup(this);
group->addAction(welcomeAct)
group->addAction(editAct)
group->addAction(designAct)
...
Simplest way - is to use QtCreator's library libCorePlugin.so and corresponding includes (FancyTabBar.h) from QtCreator's srcs
You can most likely do it by putting everything into a QHBoxLayout, where the left hand side is a QVBoxLayout column of QPushButton's with icons matching what you want. Have the buttons trigger what the right hand pane looks like.
There is also the QTabBar which does most of this work for you. You just need to tell it to put the tabs on the left hand side.