I am writing an application with a custom window shell. The shell has rounded corners and transparency. Here is the sample code of how I am doing this:
MyWindow::MyWindow (void) : QMainWindow (NULL, Qt::FramelessWindowHint)
{
setAttribute (Qt::WA_TranslucentBackground);
setAttribute (Qt::WA_NoSystemBackground );
}
The problem is whenever I use WA_TranslucentBackground with FramelessWindowHint, the font rendering becomes awful, see image below. I have a custom application style set through a global css. I tried using other fonts such as Segoe UI however that font also becomes changed.
Any ideas on why this is happening and what I can do to fix this problem. I am using C++ with Qt 5.0.2
It looks like I may have found a solution. First of all, you can use QWidget::setMask in order to get rounded corners if you prefer to not use Qt::WA_TranslucentBackground. Here is the sample code I came up with:
void MyWindow::setVisible (bool visible)
{
// Call the default event
QMainWindow::setVisible (visible);
// Set a rounded mask (size() needs to be correct)
QBitmap t (size());
t.fill (Qt::color0);
QPainter p (&t);
p.setBrush (Qt::color1);
p.drawRoundedRect (rect(), 5, 5);
setMask (t);
}
For transparency you have to make the font prefer antialiasing. You can put this at the start of the application.
QFont font = QApplication::font();
font.setStyleStrategy (QFont::PreferAntialias);
QApplication::setFont (font);
Not perfect but it fixed the problem I was having.
Related
I have an application with QMainWindow in which I insert my own widget, inherited from QGraphicsView. As viewport I use QGLWidget. Everything works fine but with Hidh DPI there is a problem: my widget(inherited from QGraphicsView) is very small.
Before creation of QApplication I enable High DPI support by
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
and in my widget I do following (from signal that comes from QMainWindow deep in the code):
void MyWidget::onNeedResize(QRect newGeom)
{
// some logic, that not interact with GUI stuff
setGeometry(newGeom);
setSceneRect(QRect(QPoint(0, 0), newGeom.size()));
// more logic, that not interact with GUI stuff
}
What did I missed? Where is a problem?
UPD1: I replaced QGLWidget by QOpenGLWidget and everything started work just as expected! Without any modifications/calculations/additional stuff. Setting of the flag is enough. But the problem is I can't use QOpenGLWidget instead of QGLWidget for now.
My assumption on why the dpi scaling does not work is because you use OpenGL widget as your viewport. From Qt docs:
Applications mostly work with device independent pixels. Notable exceptions are OpenGL and code that works with raster graphics.
From that document, it means that OpenGL-content widgets will not be scaled even if you use Qt::AA_EnableHighDpiScaling.
Try to use devicePixelRatio() directly in your resizing code. An example on how it can be used within your code:
void MyWidget::onNeedResize(QRect newGeom)
{
// some logic, that not interact with GUI stuff
setGeometry(QRect(newGeom.x() * Application::desktop()->devicePixelRatio(),
newGeom.y() * Application::desktop()->devicePixelRatio(),
newGeom.width() * Application::desktop()->devicePixelRatio(),
newGeom.height() * Application::desktop()->devicePixelRatio() ));
setSceneRect(QRect(QPoint(0, 0), QSize(
newGeom.width() * Application::desktop()->devicePixelRatio(),
newGeom.height() * Application::desktop()->devicePixelRatio() ) ));
// more logic, that not interact with GUI stuff
}
That is, for every sizing/position you use within your widget, use a scaling factor of Application::desktop()->devicePixelRatio(). That should fix your problem.
This might be very complicated question not many people know answer but I still will ask.
I do have QWindow derived class, with overloaded event(), which uses Backing store and fill in whole window with a colour, let say black.
Now I have my QT QML app, when I create my window and set parent as main view of my app, I getting window sized to 1x1px ! This is driving me crazy..
I dug though the QT source code and found this:
void QQnxRasterWindow::adjustBufferSize()
{
// When having a raster window we don't need any buffers, since
// Qt will draw to the parent TLW backing store.
const QSize windowSize = window()->parent() ? QSize(1,1) : window()->size();
if (windowSize != bufferSize())
setBufferSize(windowSize);
}
void QQnxRasterWindow::setParent(const QPlatformWindow *wnd)
{
QQnxWindow::setParent(wnd);
adjustBufferSize();
}
Which is kinda bummer because I have no idea how I suppose to use TLW and draw into my window now.
Any ideas?
First of all what is TLW ?
Second how do I draw into parent TLW in a way it will end up in my window buffer.
Thank you
QT 5.3.1
Edit:
not renderNow() - my mistake,
overloaded function event, which uses event UpdateRequested to draw my background.
Edit2:
Also this is only problem when I set parent, when no parent set I can do whatewer I want with my QWindow and it has own buffer. Kinda weird.
I'm updating an MFC dialog with a number of buttons on it.
At present, the dialog has a Picture control covering the whole dialog providing a patterned background. On top of that, each button is a CBitmapButton using (opaque) images carefully generated to match the area of background they cover.
It would obviously be much easier if the images could be created as mostly transparent, so the background shows through automatically. However, I can't work out how to get MFC to render transparent images correctly in this case.
I understand that I might want a different class to CBitmapButton, or need to write a custom subclass; that's fine, but I don't know where to start. It would be nice to support 32-bit BMP or PNG with alpha channel, but I'd settle for the "specified colour should be transparent" type.
It may not be the best way to do it, but what I'd do is create a custom CButton derived class (assuming that you're actually using the rest of the CButton functionality), then override the DrawItem function to put your custom draw code in.
For the image itself I'd use a Bitmap GDI+ object (which will allow you to load either BMPs or PNGs with alpha channels) then use the regular DrawImage function to draw the bitmap.
If you're going to put PNGs into your resource file then you need to put them in as a "PNG" type. Make sure when you look in the resource file code that the entry looks like
IDB_PNG1 PNG "C:\temp\test.png"
and doesn't try to treat it as a BITMAP resource otherwise you'll have problems loading them.
Edit
Putting my response here so I can post code. Yes, I meant to derive a custom class from CButton, then add a Gdiplus::Bitmap member variable. Here is roughly what you'll need to do to get it to work, though I haven't checked that the code actually compiles and works, but hopefully you'll get the idea. It's not the most efficient way to do it, but if you've not done much custom drawing before then it does have the advantage of being simple!
void CMyButton::LoadImage(const int resourceID)
{
m_pBitmap = Gdiplus::Bitmap::FromResource(NULL, MAKEINTRESOURCE(resourceID));
ASSERT(m_pBitmap);
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
CRect rcClient;
GetClientRect(&rcClient);
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
// If you want to do anything special when the button is pressed, do it here
// Maybe offset the rect to give the impression of the button being pressed?
rcClient.OffsetRect(1,1);
}
Graphics gr(lpDrawItemStruct->hDC);
gr.DrawImage(m_pBitmap, rcClient.left, rcClient.top);
}
I have a splash screen image that I display with splash.showFullScreen() but it doesn't re size it to the screen resolution so it either comes out tiled or to large depending on the display. I have tried everything I can think of but nothing works. This might sound like a stupid question which it probably is but I can't find the answer so if any can just help me with this? If it makes a difference I use a QPixmap named pixmap for the splash image. By the way I want the image to be stretched to the screen resolution.
You should scale the pixmap to the size of the screen with QPixmap::scaled(). You can get the screen resolution by calling QDesktopWidget::screenGeometry(). The desktop widget can be obtained by QApplication::desktop().
You can try something like this:
QDesktopWidget* desktopWidget = qApp->desktop();
QRect screenGeometry = desktopWidget->screenGeometry();
int screenWidth = screenGeometry.width();
int screenHeight = screenGeometry.height();
QPixmap pixmapForSplash = yourPixmap.scaled(screenWidth, screenHeight);
QSplashScreen splashScreen(pixmapForSplash);
(I'm sorry, I can not check this, because I do not have a development environment on this computer... I hope it is correct.)
I think you should call resize() method for your splash screen by the size of the available desktop geometry that you can get using QDesktopWidget::availableGeometry method. The QApplication::desktop() function is used to get an instance of QDesktopWidget.
slpashScreen.resize(QApplication::desktop()->avaiableGeometry().size());
If you use a QLabel to display the image, make sure the label is in a layout that will cause it to fill the entire parent widget and set the label to scale its contents using setScaledContents(true).
I'm trying to create an SDL drawing canvas inside of a simple QT4 window, following the information provided in the SDL wiki and in another question on this site. The project is an NES emulator using QT and SDL that a friend and I decided we wanted to try creating.
Currently, I have a main_window class that will contain the SDL widget, menus that I set up, and probably other stuff as the project develops. The SDL widget I'm creating is called rom_canvas and inherits from QWidget. So far, I'm able to set the SDL_WINDOWID environment variable and I seem to be able to interact with the widget in that I can set and get its geometry and see that it is in fact "visible", but nothing actually shows up in the window.
I don't have any experience with QT4 and SDL until now and don't have a ton of C++ experience, so I could be missing something obvious.
Here's the rom_canvas class:
#include "rom_canvas.hpp"
#include <iostream>
#include <cstdlib>
#include <QString>
rom_canvas::rom_canvas(QWidget *parent)
: QWidget(parent)
{
parent->setAttribute(Qt::WA_PaintOnScreen);
parent->setAttribute(Qt::WA_OpaquePaintEvent);
// setAttribute(Qt::WA_PaintOnScreen);
// setAttribute(Qt::WA_OpaquePaintEvent);
setUpdatesEnabled(false);
// a hack I found online to get the SDL surface to appear in our own window
QString id;
id.setNum(parent->winId());
setenv("SDL_WINDOWID", id.toAscii().data(), 1);
SDL_InitSubSystem(SDL_INIT_VIDEO);
resize(320, 240);
// change constants later
sdl_screen = SDL_SetVideoMode(320, 240, DEFAULT_BPP, SDL_SWSURFACE);
if(!sdl_screen)
std::cout << "couldn't create screen" << std::endl;
SDL_LockSurface(sdl_screen);
SDL_FillRect(sdl_screen, NULL, 0x00FF0000);
SDL_UnlockSurface(sdl_screen);
SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);
}
rom_canvas::~rom_canvas()
{
// do NOT release sdl_screen here; that's done when SDL_Quit() is called in main().
}
// this method is a protected slot
void rom_canvas::test()
{
std::cout << "rom_canvas test" << std::endl;
SDL_LockSurface(sdl_screen);
SDL_FillRect(sdl_screen, NULL, 0x00FF0000);
SDL_UnlockSurface(sdl_screen);
SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);
}
And here's the main_window constructor:
main_window::main_window(QWidget *parent)
: QMainWindow(parent)
{
canvas = new rom_canvas(this);
setCentralWidget(canvas);
canvas->setGeometry(100, 100, 100, 100);
canvas->show();
canvas->update();
// Add File Menu
open_action = new QAction(tr("&Open..."), this);
open_action->setShortcuts(QKeySequence::Open);
open_action->setStatusTip(tr("Open a new ROM"));
connect(open_action, SIGNAL(triggered()), this, SLOT(select_rom()));
exit_action = new QAction(tr("E&xit"), this);
exit_action->setStatusTip(tr("Exit nesT"));
connect(exit_action, SIGNAL(triggered()), /*QApplication::instance()*/canvas, SLOT(/*quit()*/test()));
// Remember to change this back!!
file_menu = menuBar()->addMenu(tr("&File"));
file_menu->addAction(open_action);
file_menu->addAction(exit_action);
rom_dir = QDir::homePath();
}
The code's a bit messy since I've been trying things to get this to work. Any help is of course greatly appreciated.
I guess you already know, but the SDL_WINDOWID trick probably isn't portable - I am pretty sure it won't work on Mac. I am curious what you want SDL for, in this scenario - if you simply want a pixel-addressable image buffer (or several), use QPixmap / QImage and stick to pure Qt. Or if you want the SDL sprite features, I'd suggest having SDL composite to an in-memory buffer attached to a QImage, and then draw that to a Qt widget. (Or use QImages as sprites, and use the OpenGL QPainter backend)
Internally, Qt works pretty hard to use platform-native code to do image format conversion, so even though this sounds fairly copy-heavy, I think it will be sufficiently fast, and much more robust and portable than the SDL_WINDOWID trick.
So I think I got it to at least display something. After playing with the code for a little bit, I ended up commenting out the setAttribute methods at the top of the file and it seems to be working. Also, the setUpdatesEnabled method didn't seem to have an impact, so I removed that since I only had it in there when trying to get this to work.
Below is a picture of it in action. I don't yet have code to refresh the surface, so I have to select a menu option to draw it for now. It does appear that the menu bar is cutting top of the surface off, so I'll have to figure out how to reposition it later.
Hopefully, this helps someone.
SDL canvas http://img18.imageshack.us/img18/3453/nestsdl.png