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);
Related
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()
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();
}
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
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.
I'm making simple 2D game (maze) and I am using QGraphicsView, QGraphicsScene and QGraphicsGridLayout to manage tiles into grid.
Map is represented by string (# is wall, . is empty field...).
So I'm going through this string, creating labels with pixmap and inserting them into grid layout:
QLabel *label;
QGraphicsProxyWidget *widget;
...
for (..) {
for (..) {
label = new QLabel();
label->setPixmap(QPixmap(":/new/images/img/w.png"));
widget = scene->addWidget(label);
layout->addItem(widget, i, j);
}
}
where scene is QGraphicsScene and layout is QGraphicsGridLayout. It works fine but I have problems with transparent background of pixmaps. Each pixmap consists of some shape and transparent backgroud. When I try to display label on main widget (QDialog window), everything is ok. But after inserting into the scene, its background is grey.
I tried it without grid layer (only graphicsview and scene) but result was same.
Is there any way to set background of scene's items transparent?
You can set the stylesheet of the label to have transparent background and no border:
label->setStyleSheet("background: transparent; border: none");