Qt remove title bar from all widget and dialog in application - qt

I know how to remove title bar from widget or QDialog, But I want to know is there any way to omit the title bar from all of the widget in application with single code,like QApplication.setWindowFlags(Qt::CustomizeWindowHint); (for example)

I usually I'm in need to do so, I usually do it with
Qt::Window | Qt::FramelessWindowHint
It should be used in the very top of your main function so you should end with something like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
WindowName window;
window.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
window.show();
}

Related

How to play a video on a translucent QWidget?

I want to play a movie on a QWidget that has Qt::FramelessWindowHint flag and Qt::WA_TranslucentBackground attirbute using QVideoWidget or QGraphicsVideoItem. But the video is not visible. I hear only sound. What is the problem?
Edit:
#include "videoplayer.h"
#include <QtWidgets/QApplication>
#include "qboxlayout.h"
#include "qvideowidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *w = new QWidget;
w->setWindowFlags(Qt :: Window | Qt::FramelessWindowHint );
w->setAttribute(Qt::WA_TranslucentBackground, true);
w->setMinimumSize(300,200);
QVideoWidget *videoWidget = new QVideoWidget;
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(videoWidget);
w->setLayout(controlLayout);
QMediaPlayer mediaPlayer;
mediaPlayer.setVideoOutput(videoWidget);
mediaPlayer.setMedia(QUrl::fromLocalFile("C:/1.wmv"));
videoWidget->show();
mediaPlayer.play();
w->show();
return app.exec();
}
I solve the problem .when we set the WA_TranslucentBackground flag and FramelessWindowHint attribute the QVideoWidget's QPainter going to QPainter::CompositionMode_DestinationOver mode and it cause to nothing to show or a shadow on the screen . in this case i use a custom video widget and in paintEvent after createing QPainter painter(this); add
painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination); or
painter.setCompositionMode(QPainter::RasterOp_SourceAndDestination);
to change the composition mode.
I've implemented VideoWidget some time ago. The only thing you shall change is your video path and set FramelessWindowHint flag.
You can find source here.

QListWidget Drag after scrolling down the list

I am creating A Sortable List in Qt. The Code works perfectly well for Downward Scroll but when i having some issues getting the draggable item after i scroll the list down. I have added some test case screenshot for better understanding
Well this is the test case code
#include <QtGui>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QListWidget *listWidget = new QListWidget;
for(int i=0;i<100;++i){
listWidget->addItem("SongOne");
listWidget->addItem("SongTwo");
listWidget->addItem("SongThree");
listWidget->addItem("SongFour");
listWidget->addItem("SongFive");
}
listWidget->setDragDropMode(QAbstractItemView::InternalMove);
listWidget->setDragEnabled(true);
listWidget->setAcceptDrops(true);
listWidget->setDropIndicatorShown(true);
listWidget->viewport()->setAcceptDrops(true);
listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
listWidget->show();
app.exec();
delete listWidget;
return 0;
}
Thankz for taking the time in reading my post. Do help me if you have any hint on what i am missing out.I think i am missing setting some property. In the main Program(not the test code), i tried rewriting the dragMoveEvent and few more method, but no use.
the problem here is not the drag and drop but the QRect that is created while doing Drag and Drop. In Figure Two the Qrect is Created but not in Figure Four
Try to remove the lines:
listWidget->setDragEnabled(true);
listWidget->setAcceptDrops(true);
listWidget->setDropIndicatorShown(true);
listWidget->viewport()->setAcceptDrops(true);
They may interfer with the "real" Drag'n'Drop system. Reordering is typically handled in an optimized way.
Another solution could be that you subclass QListWidget and get the element's index in the drag event and the other's element's index in the drop event. So you know what to reorder and where it should be placed (before or after the second element's index). The rest should be easy, just takeItem() and insertItem().
Hope I helped you with these two ways ;)
In win7, i was not able to reproduce the same behavior. It just works(even size hint is tweaked to meet your screen shot requirement. :) )
My test code was...
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QListWidget *listWidget = new QListWidget;
const char* titles[] = {
"SongOne%1",
"SongTwo%1",
"SongThree%1",
"SongFour%1",
"SongFive%1",
};
for(int i=0;i<100;++i){
QString title = QString(titles[i%5]).arg(i);
QListWidgetItem* item = new QListWidgetItem(title);
item->setData(Qt::SizeHintRole, QSize(50,100));
listWidget->addItem(item);
}
listWidget->setDragDropMode(QAbstractItemView::InternalMove);
listWidget->setDragEnabled(true);
listWidget->setAcceptDrops(true);
listWidget->setDropIndicatorShown(true);
listWidget->viewport()->setAcceptDrops(true);
listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
listWidget->show();
app.exec();
delete listWidget;
return 0;
}

Qt: QMenu with translucent background

I use Windows and I want to set a style sheet to a QMenu to give it a translucent background. In order for that to work, I first set the FramelessWindowHint, then I set the WA_TranslucentBackground attribute. Then I set my style sheet and display the menu with the popup method. It is drawn correctly, but it behaves strangely: As soon as it has the FramelessWindowHint, it is always visible (even before calling the popup() method). It does not hide itself anymore after one of its entries has been clicked.
Here is a minimalistic example:
#include <QApplication>
#include <QMenu>
#include <QPoint>
#include <QCursor>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMenu menu;
menu.addAction("about", &a, SLOT(aboutQt()));
menu.addAction("exit", &a, SLOT(quit()));
menu.setWindowFlags(Qt::FramelessWindowHint);
menu.setAttribute(Qt::WA_TranslucentBackground);
menu.setStyleSheet("QMenu{background:rgba(255, 0, 0, 50%);}");
menu.popup(QCursor::pos());
return a.exec();
}
menu.setWindowFlags(menu.windowFlags() | Qt::FramelessWindowHint);
should solve your problem. Now you are clearing all flags already set by Qt.

QLabel consuming too much space

I use a QLabel and QPLineEdit within a QStackedWidget , the QLable should be nearly the size of the window holding this widget.
But when I set a extra long text to QLabel , it's expanding too much , and I'm not able to reduce the window size horizontally , the minimum width was too much.
I set the size policy of these three widgets to Minimum already , it just won't work for me.
UPDATE
maybe it's better saying like this: how to let QLabel display part of the text , when there's not enough space
SAMPLE CODE
#include <QtGui>
int main ( int argc , char **argv )
{
QApplication app (argc , argv);
QWidget w;
QLabel *label = new QLabel ("Very very very long text");
label->setSizePolicy (QSizePolicy::Minimum , QSizePolicy::Fixed);
QVBoxLayout layout (&w);
layout.addWidget ( label );
w.show();
return app.exec();
}
If I understand you correctly, the simplest thing to do is simply to ignore that label's horizontal size hint.
As long as you have other widgets in there (or force a minimum width manually to the container), this should do what you want:
#include <QtGui>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QLabel *l1 = new QLabel("This very long text doesn't influence "
"the width of the parent widget");
l1->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
// Style just to make it clear that the widget is
// being resized to fit the parent, it doesn't "overflow"
l1->setFrameShape(QFrame::Box);
l1->setFrameShadow(QFrame::Raised);
l1->setAlignment(Qt::AlignHCenter);
QLabel *l2 = new QLabel("This influences the width");
l2->setFrameShape(QFrame::Box);
l2->setFrameShadow(QFrame::Raised);
QWidget w;
QVBoxLayout layout(&w);
layout.addWidget(l1);
layout.addWidget(l2);
w.show();
return app.exec();
}

QT: Hide the title bar of a dialog/window

I have a parent window in which a push-button's click event function has the following lines:
SplashScreenDialog *splScrDlg = new SplashScreenDialog(this);
splScrDlg->show();
What I want is I want to remove the maximize button, minimize button, close button and also the title bar from the dialog(or window). [Actually it is for a splash screen, it would contain an image for a while and then would exit automatically and opens the main window, you are welcome with other ideas for showing splash screen]
Why not using QSplashScreen?
Example extracted from the assistant:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPixmap pixmap(":/splash.png");
QSplashScreen splash(pixmap);
splash.show();
app.processEvents();
...
QMainWindow window;
window.show();
splash.finish(&window);
return app.exec();
}
Why not use QSplashScreen for this? Anyway, you can set window flags to remove the window decoration. See the documentation for QWidget::setWindowFlags and Qt::WindowFlags.

Resources