make quickwidget background transparent - qt

I have created 2 QWidget and 1 QQuickWidget in main window. The QQuickWidget uses a source qml file. I want to set background transparent for two QWidgets and the QQuickWidget.
Can anyone help me to make the background of the QQuickWidget transparent so that my main window is also visible.

const auto m_QuickWidgetPtr = new QQuickWidget(this);
...
m_QuickWidgetPtr->setAttribute( Qt::WA_AlwaysStackOnTop );
m_QuickWidgetPtr->setAttribute( Qt::WA_TranslucentBackground );
m_QuickWidgetPtr->setClearColor( Qt::transparent );

Related

Find border image of QMainWindow programmatically

I set a border image on my QMainWindow using a style sheet:
QMainWindow
{
border-image: url(:/metal_background);
}
In my program, there are several widgets with transparent background colors that sit on top of the main window. However when I drag and drop them, they look bad because they end up taking the system background color.
What I would like to do is get the brush(?) used to draw the mainwindow background, and use it to paint the child widget while it's being moved. I've tried:
QWidget* widget;
QMainWindow* mainWindow;
QList<QWidget*> widgets = qApp->topLevelWidgets();
foreach(widget, widgets)
{
if(widget->objectName() == "mainWindow") // name is OK
break;
}
mainWindow = static_cast<QMainWindow*>(widget);
qDebug() << mainWindow->objectName();
QPalette palette;
palette.setBrush(this->backgroundRole(),
mainWindow->palette().brush(mainWindow->backgroundRole()));
this->setPalette(palette);
this->repaint();
but it gives the background color/gradient, not the image. Am I doing something wrong, or is there another/better way to get the image?

How to display fix banner between QMenubar and QToolBar in QMainWindow

How can I show fixed banner (with some widget like label and button ) in between
QMenuBar and QToolBar ?
Similarly like QStatusBar but in between QMenuBar and QToolBar.
I tried to implement using QToolBar.
// toolbar Banner with lable inside it.
QLabel * bannerLabel = new QLabel(" bannerToobar with label banner.");
bannerLabel->setAlignment( Qt::AlignVCenter );
ui.bannerToobar->addWidget( bannerLabel );
ui.bannerToobar->setAllowedAreas(Qt::ToolBarArea::TopToolBarArea);
ui.bannerToobar->setMovable( false );
QSize banner_sz = ui.bannerToobar->size();
ui.bannerToobar->setFixedHeight( banner_sz.height() * 2 );
QSizePolicy banner_szPolicy( QSizePolicy::Policy::Maximum, QSizePolicy::Policy::Fixed );
banner_szPolicy.setHorizontalStretch(255);
ui.bannerToobar->setSizePolicy( banner_szPolicy );
but i can't prevent user from draging mainToolbar and droping in the same row as my
bannerToolbar
You can force it to wrap initially using QMainWindow::addToolBarBreak, but I don't know a way to prevent it from being put back there later by the user (except for making the toolbars non-moveable).
If there was a QToolbar::dockLocationChanged signal (which seems to have been requsted and resolved in https://bugreports.qt-project.org/browse/QTBUG-1274, but I still don't see the signal anywhere), I suppose you could use insertToolBarBreak to fix it up whenever things have changed. Maybe there's some hackish way you could get notified when the toolbars move.
Or you could use QMainWindow::setMenuWidget to place a widget containing both your QMenuBar and something else into the menu area of the QMainWindow. This could get tricky if you want to support styles (mac/gnome/etc) where the menubar gets lifted out of your window to the top-of-screen and the toolbar gets unified with the window title decorations. But the idea of a banner between menubar and toolbar just naturally has problems in such cases :-)

How to implement Menu bar inside QGraphicsView?

I have a QGraphicsView which shows images dynamically. I also used fitInView feature to resize the window. Now I need to add the menu bar at the top of the QGraphicsView. How to implement this? Please help. I am new in Qt
As beginner it is probably easiest to place your QGraphicsView into a QVBoxLayout and create a QMenuBar and insert it above your graphics view.
QMenuBar *bar = new QMenuBar();
ui->yourVerticalLayout->insertWidget( 0, bar );
QMenu* yourMenu = bar->addMenu("Your Menu title");
QAction* yourFirstAction = yourMenu->addAction("Your First Action");

Quality of image deteriorates when used fitinview and Qt:KeepAspectRatio

I am using Qt and I want to display a image and I am displaying it in QGraphicsView and as the image is bigger than the dimensions of box of the QGraphicsView I used
ui->graphicsView->fitInView(viewraw->itemsBoundingRect() ,Qt::KeepAspectRatio);
where viewraw contains pixmap address to my image, my problem of oversized image was solved with this but my new problem now is the image after automatic resizing by Qt now shows with very very bad quality, it is very very blurry
What should I do?
Is there any way to display the image fit in the box and no quality loss?
Thank you!
You should try enabling antialiasing:
ui->graphicsView->setRenderHints(QPainter::Antialiasing
| QPainter::SmoothPixmapTransform
| QPainter::TextAntialiasing);
You can use QLayout::setSizeConstraint(QLayout::SetFixedSize) on the layout containing the view, to resize the parent widget to fit the view. The size will be the one returned by the view sceneRect() function.
For example, the following code will resize the QMainWindow (and lock the size) so that both items are visible:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow mainWindow;
QGraphicsView *view = new QGraphicsView(&mainWindow);
QGraphicsScene *scene = new QGraphicsScene(view);
view->setScene(scene);
mainWindow.layout()->setSizeConstraint(QLayout::SetFixedSize);
mainWindow.setCentralWidget(view);
mainWindow.show();
scene->addRect(-100,-100,10,10, QPen(), Qt::red );
scene->addRect(600,450,10,10, QPen(), Qt::blue );
return a.exec();
}
Or, since you are using the designer,
in the widget tree, go to the parent item of the graphics view, it should have a layout,
if not (if the icon is this one: ), add one by left clicking on the item name and choose a layout in the "Lay Out" menu (if you only have one widget, the type of layout doesn't matter, just choose one),
then go to the last option in the property panel for the same item, which should be "layoutSizeConstraint" and select the option "SetFixedSize".

QT: Complex Layout and seamless window background image

With some help from you QT sages, I was able to implement this window, with the desired layout and resizeability behavior. Now I have another interesting problem.
I want my entire window to have a "repeat-xy" seamless pattern. If I apply it to a simple window without layout and internal widgets, it works perfectly. I do however, now have a "tree" of widgets within widgets, and I can't set the stylesheet to draw my seamless background image to each and every one, cause it looks unnatural. The image must be underlying to all the widget topology I have. The problem is, it's invisible when i apply it to the bottom all-window-covering widget because it has widgets on top of it.
Is there a solution? maybe "transparent widgets" that can contain visible widgets?
I made the following using only CSS, there's a QPlainTextEdit, two QPushButton and a QLineEdit. In the image i added a red border to the QPlainTextEdit only so it can be seen, the rules are the following
QWidget#Form{
background-image: url(:/img/elephant_pattern.gif);
}
QPlainTextEdit{
background:transparent;
border:1px solid red;
}
As you can see all i had to was setting background transparent in the widgets i wanted.
Write your own widget that inherits QWidget. Reimplement the paintEvent and leave it empty. The widget itself won't be drawn, but its children will be.
my_widget::my_widget( QWidget* parent ) : QWidget( parent )
{
}
void my_widget::paintEvent( QPaintEvent* p_event )
{
// left empty to let my_widget be invisible
}
test_mw::test_mw( QWidget *parent ) : QMainWindow( parent )
{
test_widget = new my_widget( this );
QHBoxLayout* layout = new QHBoxLayout();
QPushButton* button0 = new QPushButton( "Button 0", 0 );
QPushButton* button1 = new QPushButton( "Button 1", 0 );
layout->addWidget( button0 );
layout->addWidget( widget );
test_widget->setLayout( layout );
setCentralWidget( test_widget );
}
Although the paintEvent of my_widget is empty, both QPushButtons are drawn. :-)
There was a QWidget::setBackgroundOrigin() method in earlier versions of Qt.
It's all done with styles now. See the examples http://doc.qt.nokia.com/latest/widgets-styles.html

Resources