How to convert QAction inside a QMenu to QWidget? - c++

I want to set icon of QAction using qss file. I cannot set stylesheet to QAction so I want to convert it to a QWidget so that I can add icon to individual actions of QMenu using stylesheet. I know how to do that in code by I want to add Icon of QActions using qss.
QMenu* menu = new QMenu();
QAction* newFile = new QAction;
newFile->setText("New File");
menu->addAction(newFile);
I want to set icon to QAction in qss.

QMenu *menu;
menu =new QMenu();
QToolButton *button=new QToolButton(menu);
button->setFixedSize(50,50);
QWidgetAction *action=new QWidgetAction(this);
action->setDefaultWidget(button);
menu->addAction(action);
Use QToolButton instead of QAction and you can set the icon using setStyleSheet() or setIcon()

Related

Retrieve QMenu of a submenu [duplicate]

I have a list of QActions, some are added to the top level menu and few are added to submenus of top level.
Is there any way to know parent menu name of each actions?
QAction *act;
I'm trying act->parentWidget(). But how can I get menu name from this?
You can check if the result of act->parentWidget() if it is a valid pointer, if so you can manipulate as a normal widget.
To get the menu name, it depends on which widget you are using.
If is QMenu, you can retrieve the menu title via the title function.
QAction *act;
...
QWidget *widget = act->parentWidget();
if (widget) {
QMenu *menu = dynamic_cast<QMenu*>(widget);
menu->title();
}

Widget focusOutEvent event is called before button press inside widget

I have a class(MyWidget) inherited from QWidget and inside it I created a button and other widget (say W1), Both kept inside QVBoxLayout
On focusOutEvent widget(MyWidget) should hide and it works fine, but when I click the button inside the widget its getting hide but on clicking the widget (W1) inside the layout its not hiding
m_layout = new QVBoxLayout(widget);
m_clearButton = new QPushButton(widget);
m_layout->addWidget(m_clearButton,0,Qt::AlignRight)
// this widget on click main widget is not hiding
m_layout->insertWidget(m_layout->count() -1,item);
Why button click hides MyWidget

QMenu: Set text color for specific QAction

I have a QMenu as context menu that looks like this:
Menu
- information_A
- information_B
- information_C
Now i want the entry information_B to be painted in a different color. How can i archive this?
EDIT: I found the best solution in this post: link
In your case it would be as simple as:
QMenu contextMenu(this);
QString menuStyle(
"QMenu::item{"
"color: rgb(0, 0, 255);"
"}"
);
contextMenu.setStyleSheet(menuStyle);
For more options and possibilities take a look at the answer in the link I provided above.
PREVIOUS SOLUTION:
You can use QWidgetAction instead of QAction, and define your QLabel with text and stylesheet you want, and then assign it to your QWidgetAction. But keep in mind that you have to tweak the width and height of your QLabel, in order for it to look the same as QAction does.
Sample code:
// label
QLabel *text = new QLabel(QString("your text here"), this);
text->setStyleSheet("color: blue");
// init widget action
QWidgetAction *widAct= new QWidgetAction(this);
widAct->setDefaultWidget(text);
contextMenu.addAction(widAct);
If you are only looking to style a single item in the menu, you can set it as default with QMenu::setDefaultAction and style a default menu item with the QMenu::item:default selector.
I.e.:
QMenu* menu = new QMenu("My menu");
QAction* actionToStyle = new QAction("My action");
menu->addAction(actionToStyle);
menu->setDefaultAction(actionToStyle);
menu->setStyleSheet("QMenu::item:default { color: #ff0000; }");
The limitation of this method is that it can apply special styling to only one item.

Scrolling QTableView in QVBoxLayout in QScrollArea

I had a problem with QTableView widget:
I need to horizontal scroll whole widget with headers, but standart scrolling scroll only content, but not headers.
Then I tried to add QScrollArea like this (this all in QDockWidget):
class matrix : public QScrollArea {
};
in constructor:
QVBoxLayout* layout = new QVBoxLayout(this);
tableView = new QTableView(this);
tableView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
layout->addWidget(tableView);
this->setLayout(layout);
but it doesn't work properly: scrolling bar doesn't appear.
(sorry, if I break some rules - it's my first question here, and sorry for my bad english)
You could for instance remove the layout and set the QTableView directly as viewport.
tableView = new QTableView;
setWidget(tableView);
setWidgetResizable(true);
tableView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

Qt QMenu setting a stylesheet

I have a QMenu which I create like this.
QMenu *logoMenu = new QMenu();
I then add it to a QToolButton like so
logoButton->setMenu(logoMenu);
now I have an project.qss file which has the following stylesheet for the QMenu
QMenu#logoMenu {
background-color: #161614; /* sets background of the menu */
border: 0px solid #161614;
width: 150px;
position: absolute;
top: 50px;
}
QMenu::item#logoMenu {
background-color: transparent;
}
QMenu::item:selected#logoMenu {
background-color: #202020;
}
My problem is that the stylesheet does NOT get applies to the QMenu, I know I am loading the stylesheet correctly because I can style other widgets.
If I set the stylesheet manually like so
logoMenu->setStyleSheet("QMenu {.....}");
Here is how I create the QMenu:
logoMenu = new QMenu();
logoMenu->addAction(QString::fromUtf8("Import"));
logoMenu->addAction(QString::fromUtf8("Export"));
logoMenu->addAction(QString::fromUtf8("Help"));
logoMenu->addAction(QString::fromUtf8("Exit"));
logoMenu->setObjectName("logoMenu");
It works, no problem. Does anyone know why this happens?
Your Id selector is QMenu#logoMenu
It means Matches all "QMenu" instances whose object name is
"logoMenu". So you need to set object name of QMenu(
yourMenu->setObjectName( "logoMenu" ) )
Note:If QMenu is created using ui designer, Ui compiler will generate the code for yourMenu->setObjectName( "logoMenu" ). But if you are creating your own QMenu, you need to set object name explicitly to work your style type.
Judging from your comments under Ashifs answer you are missing a connection inside your Qt object tree. Try instantiating your QMenu with a parent that has a connection to the stylesheet. Probably just:
QMenu *logoMenu = new QMenu(this);
Alternatively you can set your stylesheet application global by adding it to the QApplication instance:
qApp->setStyleSheet("...");