I want to make a mask widget(top-level, means that window) with opacity 0.75 on linuxfb.
As it is a top-level widget(means that no parent).
I cannot use stylesheet "background-color:rgba(0,0,0,75%)".
I have tried:
MaskWidget::MaskWidget() {
setWindowFlags(Qt::FramelessWindowHint);
1.
setWindowState(Qt::WindowMaximized);
this->setWindowOpacity(0.75); //show warning "This plugin does not support setting window opacity"
2.
QRect desktopRect = QApplication::desktop()->availableGeometry(this);
setGeometry(0, 0, desktopRect.width(), desktopRect.height());
setAttribute(Qt::WA_TranslucentBackground); // it seems that not work
QGraphicsOpacityEffect* oe = new QGraphicsOpacityEffect();
oe->setOpacity(0.75);
this->setGraphicsEffect(oe);
}
The above methods work well in ubuntu and windows, but in my device with QPA linuxfb not work.
Related
I meet two problems, I searched, but they seems not so easy as I think.
I'm working on ubuntu-18.04 with qt-5.15.x. And my two problems are:
cannot restore my window after maximizing it.
cannot move my window out of my screen with mouse dragging
Can not restore
Firstly, I set my window frameless, and then using a button standing for maximize or restoreoperations to trigger maximization or normal
// set fremeless in contruction
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
// maximaization slot or restore
void WindowTitle::onButtonMaxClicked()
{
QWidget *pWin = window();
if(pWin->isMaximized())
{
pWin->showNormal();
}
else
{
pWin->showMaximized();
}
}
What I expected is when I click button ,window will maximized, and that is true indeed. Then clicking on button again, window will return original position and size again, but that doesn't happen, instead, window's height is correct, but its width is equal screen(getting rid of applications docker bar), x-axis value is wrong too. And I have tried setGeometry after I have stored its value, but failed.
Can not move outside
I can make sure that I have set correct position with
void WindowTitle::mouseMoveEvent(QMouseEvent *event)
{
if (m_isPressed)
{
QPoint movePoint = event->globalPos() - m_startMovePos;
QPoint widgetPos = QApplication::activeWindow()->pos();
m_startMovePos = event->globalPos();
QApplication::activeWindow()->move(widgetPos.x() + movePoint.x(), widgetPos.y() + movePoint.y());
}
return QWidget::mouseMoveEvent(event);
}
function, but the result shows something wrong.
I have searched above two questions. But get nothing. And I'm curious that other applications can do just right both restore and move, suchlike Firefox web browser(which I'm typing on). So, there must be some way to make it work.
I need to be able to print from qt (the rendered contents of a QGraphicsScene, or a QImage), to scale, on normal printer, pdf, but also on custom printers, including roll fed.
It seems that anything that works for standard printers fails on custom printers, and the reverse.
I have made the printing work as expected on custom printers now (going back and forth between what works in the different printers).
I set the custom size required, and the preferred orientation, based on length/width ratio.
I open a print dialog (and even check the supply- the paper is set to desired size, and the orientation is set as expected)
print:
On custom printer, I get the correct size, and if the supply is smaller, the print clips as needed. margins are set correctly too.
On Pdf, I get a document of custom size as requested, printed correctly - but the orientation is not respected !!! (even though print dialog showed it correctly) - see image
On HP printer, I get a white page - nothing gets printed.
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QPrinter>
#include <QPrintDialog>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// some scene to print - show rectangle for easy measure
QGraphicsScene* s = new QGraphicsScene();
s->setSceneRect(-500, -500, 1500, 1500);
s->setBackgroundBrush(Qt::red);
QGraphicsView* view = new QGraphicsView();
view->setScene(s);
view->show();
qreal in = 72;
QRectF canvasRect(-0.1*in, -0.1*in, 6*in, 4*in);
qreal margins = 0.1*in;
QRectF actualCanvasRect = canvasRect.adjusted(margins,margins,-margins,-margins);
// this is to show actual scene
QGraphicsRectItem* contourItem = new QGraphicsRectItem(actualCanvasRect);
contourItem->setBrush(Qt::blue);
s->addItem(contourItem);
// an item partially on canvas (so I can check margins)
QGraphicsRectItem* item = new QGraphicsRectItem(-.5*in, -in, 2*in, 3*in);
item->setBrush(Qt::yellow);
s->addItem(item);
// actual printing:
// print the scene, to scale, using user margins, on given printer
QPrinter printer;
QPrinter::Orientation orient = (actualCanvasRect.width() >
actualCanvasRect.height() ?
QPrinter::Landscape : QPrinter::Portrait);
printer.setOrientation(orient);
printer.setPaperSize(canvasRect.size(), QPrinter::Point);
printer.setPageMargins(margins, margins, margins, margins, QPrinter::Point);
QPrintDialog printDialog(&printer);
if (printDialog.exec() != QDialog::Accepted)
{
qDebug("dialog canceled");
return 1;
}
QPainter painter;
if (! painter.begin(&printer))
{
qDebug("failed to open printer");
return 1;
}
// render the contents, clipped to printer page size, and scaled from point to device pixel
QRectF source = actualCanvasRect;
// convert target rect to DevicePixel and clip to page
QRectF page = printer.pageRect(QPrinter::DevicePixel);
qreal scale = printer.resolution()/in;
QRectF target = QRectF(page.topLeft(), source.size() * scale);
target &= page; // clip target rect to page
// clip source rect to page - without this, if printer paper is smaller I get unwanted scaling
source &= printer.pageRect(QPrinter::Point);
s->render(&painter, target, source);
painter.end();
return app.exec();
}
I don't understand why pdf creates a portrait page even though I clearly requested landscape (without changing the print dialog: see image). (the width and height are reversed, yet correct - Document Properties shows 4x6, and the page attempts to print correctly and to scale)
More important, I don't understand why a typical laser jet printer prints nothing - blank page - or sometimes for a simple canvas it scales to fit.
BUT if I change any properties in the print dialog from HP, anything irrelevant (like paper source, or paper type.... ), it prints correctly.
What am I doing wrong ?
(using Qt 4.7 and 5.5, must work on 4.7 - Windows, have yet to try Linux)
We used to use trick https://www.qt.io/blog/2009/06/30/transparent-qwebview-or-qwebpage to make QWebView transparent with Qt4 as following, but same code give us blank background with Qt 5.2. Is there any way I can make that work with Qt5?
setAttribute(Qt::WA_TranslucentBackground, true);
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
if(m_pWebView)
{
QPalette palette = m_pWebView->palette();
palette.setBrush(QPalette::Base, Qt::transparent);
m_pWebView->page()->setPalette(palette);
m_pWebView->setAttribute(Qt::WA_OpaquePaintEvent, false);
m_pWebView->setContextMenuPolicy(Qt::NoContextMenu);
}
The following two lines worked for me under QT5.4
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("background:transparent");
How to Make Dynamic Screen of different size in QT for Nokia Devices in QT Creator?
Just set fullscreen window state on your topmost widget:
w->setWindowState(w->windowState() ^ Qt::WindowFullScreen);
The fullscreen window fills the entire screen without any frame around it.
do it in this way it works fine:
MyMainWindow::QMyMainWindow(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QFrame* frame = new QFrame(this);
setCentralWidget(frame);
frame->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QRect rect(0, 0, 240, 320);
frame->setFrameShape(QFrame::Box);
frame->setLineWidth(3);
frame->setFrameShadow(QFrame::Plain);
frame->setGeometry(rect);
adjustSize();
}
I didn't specify main window size at first and it would be totally expanded by central widget.
I also tried other SizePolicy values, but the resulting size of main window was not (240, 320) as expected at all.
Seems to be possible with native controls (see here and here) so now I'm looking for some Qt code to do it.
I use this code for the second display in full screen successfully on both Windows & Linux
QRect screenres = QApplication::desktop()->screenGeometry(1/*screenNumber*/);
SecondDisplay secondDisplay = new SecondDisplay(); // Use your QWidget
secondDisplay->move(QPoint(screenres.x(), screenres.y()));
secondDisplay->resize(screenres.width(), screenres.height());
secondDisplay->showFullScreen();
One way of doing it in Qt5 is to use QWindow::setScreen to set the screen on which the window should be shown. QWidget has a windowHandle() that returns the pointer to the QWindow.
Here is how to show your widget on second screen in full-screen mode :
QWidget * widget = new QWidget();
widget->show();
widget->windowHandle()->setScreen(qApp->screens()[1]);
widget->showFullScreen();
My take on this:
auto const desktop(QApplication::desktop());
setGeometry(desktop->screenGeometry(1));
#ifndef Q_OS_WIN
setWindowState(Qt::WindowState(Qt::WindowFullScreen | windowState()));
#endif // Q_OS_WIN
showFullScreen first, then setGeometry.
Qt5 tested OK
This problem got solved while using window->showFullScreen() instead of window->show().