Widget focusOutEvent event is called before button press inside widget - c++

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

Related

How to convert QAction inside a QMenu to QWidget?

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()

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();
}

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);

SDI-display a dialog via a seleted popup menu item

I have a SDI application and I would like to display a dialog after selecting a popup menu item to call it
My dialog class is defined as:
class Dialog:public CDialogEx
{};
and an added function to view class named OnCallDlg does something as simple as:
void CAppView::OnCallDlg()
{
Dialog d;
d.DoModal();
}
But there is nothing shown up after I choose an item in the popup menu when rightmouse clicking the view.
You have to attach the ID to your dialog using following pattern:
Dialog d(ID_DIG);
d.doModal();