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");
Related
Image of QFrame issue:
I am putting a frame around the window and my window is having a scrollbar (vertical scrollbar only).
I have set the QFrame styling as
QFrame {
border: 8px solid gray;
}
and for styling of scrollbar I have used customizing-qscrollbar.
Now I am seeing that there a white line at the bottom of the scrollbar next to the down arrow. The same line is not there above the up arrow of scrollbar.
Seems like a corner widget was causing the above issue.
Since i found the fix, i would like to share.
QAbstractScrollArea::corner {
background: none;
border: none;
}
I have a QMenuBar in window, and background color is white, so when action is selected, or mouse is point at action, text color becomes white.
How can i change hover effect color? I tried to change from pallete by changing selected text color, but that doesn't worked
QMenuBar::setStyleSheet()
will do the trick.
You can fully customize the layout of your component. Be aware of using setStyleSheet means you'll completely override the style of the component with your stylesheet.
QString style = "QMenuBar::item:selected { background: white; } QMenuBar::item:pressed { background: white; }"
menuBar.setStyleSheet(style);
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);
I intended to make a un-rectangle QDialog, so I paint a png image by override QDialog::paintEvent.Everything is okay except there is a gray border shown arrond the dialog.Like this:
I am sure that border is not belong to the image,and I had setWindowFlags(Qt::FramelessWindowHint) and setAttribute(Qt::WA_TranslucentBackground, true).I tried to set a qss like border-width: 0px but didn't work.
Is there any way to remove the border?And why it is shown?
You can create a borderless dialog by setting Qt::FramelessWindowHint window flag :
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
To make it transparent you should set these attributes :
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_PaintOnScreen);
I have a Qt QMenu in my app, consisting of two levels (the top level of submenus, then each submenu containing actions), and we have a custom dark-grey style whereby the menu background is grey, the text color is white, and the right arrow is white. When a submenu item is highlighted (mouse-over), the item background is white, the text is black, and I want the right arrow to switch to a black image as well. I'm using a CSS stylesheet to do this. However, I can't seem to find the right syntax to set an alternate right-arrow image for the item selected state. My CSS looks like this:
QMenu
{
background-color: rgb( 24, 24, 24 );
color: white;
}
QMenu::item:selected
{
background-color: white;
color: black;
}
QMenu::right-arrow
{
image: url(Resources/MenuRight.png);
}
I've tried the following additions after the above code (where MenuRightSelected.png is an inverted-color image of MenuRight.png):
QMenu::right-arrow:selected
{
image: url(Resources/MenuRightSelected.png);
}
and
QMenu::item::right-arrow:selected
{
image: url(Resources/MenuRightSelected.png);
}
Neither of these affect the displayed QMenu. Does anyone know if it is possible to do what I'm after, and if so, how?
Possibly you only have to change the order of the items in your css file. I once heard that Qt parses the css files from upwards. Items further up overwrite the behaviour of items further down.