How to draw to "parent TLW backing store"? - c++

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.

Related

How do I actually use OpenGL in Qt 5?

Simply put, I cannot for the life of me figure out how to actually make use of things like QOpenGLWidget or QOpenGLWindow or anything. I want to have the rendering I do be a child widget of a window in a MDI, but nothing works.
Here's the code I have currently set up for the widget (at least, just the parts involving OpenGL):
Viewport::Viewport(QWidget * parent) : QOpenGLWidget(parent) { }
void Viewport::initializeGL() {
initializeOpenGLFunctions();
}
void Viewport::paintGL() {
// first, clear the screen
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
And here's how I use the widget:
vp = new Viewport;
vp->resize(QSize(320,240));
hbox->addWidget(vp);
And the result is that I see nothing. I just see a tiny sliver of empty space next to the other widget in the window, but that's it. No black screen like I try to clear with, not even a 320x240-sized empty space.
Like I said, I've been unable to do this in any of the ways I could find, and it's really frustrating. Am I missing something obvious? There's very little documentation as-is, so it's hard to tell if I am, or if there's some weird corner case I'm running into. (For example, none of the documentation I find uses QOpenGLWidget as part of a larger widget; is that because it can't be, or because all the examples I can find are just lazy about using the widget as its own top-level window?)
After some more fiddling around, it turns out my issue was apparently caused by the other object in the window (a QListView) by default taking up as much space as possible, making the OpenGL widget disappear since it doesn't have a minimum size.
In other words, the problem is fixed by either changing the QListView to have a QSizePolicy::Preferred sizing policy (since resizing the window will now let you see the OpenGL widget), or by giving the OpenGL widget a minimum or fixed size.
(As an aside, I really wish this could've been more obvious than just stumbling upon it by chance.)

Qt FramelessWindowHint and WA_TranslucentBackground Font Rendering

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.

How to display QWidget above data stream from device handled by external library

I'm creating application to analyze data from a device and displaying it on the screen. SDK to handling this device have function to display current frame of data in specific Window by setting window handler (HWND).
In my Qt Gui Application i'm trying display my own widget over video stream, which a DLL showing in my QGLWidget (it's set by winId function and HWND). MainWidget is a parent of QGLWidget where the data stream is displayed, and QWidget (or some graphic marker, for example Circle) should be displayed above data stream from QGLWidget.
Everything works almost perfect, but I'm getting blinking effect (twinkle effect?) - my circle widget is hidding and showing with frequency which human eye can get and i try to avoid it. The only option to eliminate that is create this circle as widget and set for its Qt::Popup flag, by it has big disadvantage - i don't have access to the rest of interface (I know, it's Popup flag fault). I've tried other options like:
set Qt::WindowStaysOnTopHint and few other flags,
create layout object which parent is QGLWidget, where i'm displaying data from device, and than set circle widget as item of layout, but here black background is shading displayed data (i've turned off even background, but i've realized that Qt can't know what is under my widget because it's handled by external library).
In documentation i found information that i can create my own directshow+COM object (or interface, am i right?) to handling video stream but i don't really know this technologies and i want avoid this option so much!
[Edit] I found that i can take current frame of data as IPictureDisp interface, but as I said earlier i don't really know COM technology. I've tried to find something about, how work with IPictureDisp but i don't have basic knowledge about COM technology. Does anybody has any good tutorial about it?
Try this widget hierarchy:
MainWindget
|-QGLWidget
|-MarkerWidet
MarkerWidet should be a small square widget.
Make sure that MarkerWidet is above QGLWidget by calling MarkerWidet::raise().
But call MarkerWidet::lower() before calling MarkerWidet::raise(). Qt has an error with QWidget::raise(), this error is eliminated by calling lower(); raise();.
After starting your application check actual widget hierarchy, use Spy++ to do it. Spy++ can be found in the Visual Studio installation, or downloaded here.
Pseudocode:
MainWindget* mainWidget = new MainWindget;
QGLWidget* glWidget = new QGLWidget(mainWidget);
device->setHwnd(glWidget->winId());
mainWidget->show();
...
MarkerWidet* marker = new MarkerWidet(mainWidget);
marker->resize(30, 30);
marker->move(10, 10);
marker->lower();
marker->raise();

pyqt : Why QGLWidget influenced by maya event?

openGL with maya
I made a openGL View with QGLWidget. It has a problem when work with Maya. As you see in video, when I click and move on a modelPanel within Maya, the QGLWidget get broken. And I found that the marquee rectangle is shown on my QGLWidget, not Maya. Why this problem happend?
To datenwolf
I tried to edit my code as you suggested. But... it doesn't call makeCurrent() and doneCurrent() at all. I expected that when I clicked on my maya modelPanel it could send me the message, but it didn't. What did I miss? Sorry for that.
def makeCurrent(self):
import OpenGL.WGL as wgl
print "MAKE CURRENT!!!"
self.prevHDC = wgl.wglGetCurrentDC()
self.prevHRC = wgl.wglGetCurretnContext()
super(GLWidget, self).makeCurrent()
def doneCurrent(self):
import OpenGL.WGL as wgl
print "DONE CURRENT!!!"
super(GLWidget, self).doneCurrent()
wgl.wglMakeCurrent(self.prevHDC, self.prevHRC)
I wrote just like above. But it never even show the "MESSAGE".
Most likely Qt and Maya's event loop are battling for processing event. Qt's paintGL does the right thing and makes the OpenGL context current whenever it is called. Maya however does not and so drawing commands of Maya end up in your OpenGL context.
Playing along with Maya is going to be tricky, because it requires storing which OpenGL/DC context was active before switching and restoring that once finished with one own's operations. You'll probably have to subclass QGLWidget and QGLContext to do this.
Update due to comment
Derive from QGLWidget, add two members HDC m_prevHDC and HRC m_prevHRC, override makeCurrent and doneCurrent
void QMyGLWidget::makeCurrent()
{
this->m_prevHDC = wglGetCurrentDC();
this->m_prevHRC = wglGetCurrentContext();
QGLWidget::makeCurrent();
}
void QMyGLWidget::doneCurrent()
{
QGLWidget::doneCurrent();
wglMakeCurrent(this->m_prevHDC, this->m_prevHRC);
}
Then derive your actual GLWidget from this intermediary class.

Trying to make SDL widget in QT4 using SDL_WINDOWID, but can't get widget to show

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