Compilation issue with qt_screen - c++

I have Qt5.5 Installed on Desktop PC On Ubuntu OS.
While compiling my program i am getting below error -
This is what i got for error
../../work/mainwindow.cpp: In constructor 'MainWindow(QWidget)':
../../work/mainwindow.cpp:63:31:
error: 'qt_screen' was not declared in this scope ui->stackedWidget-
>resize(qt_screen->deviceWidth(),qt_screeb->deviceHeight());
And this is part of my code
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setipUi(this);
setWindowFlags(Qt::CustomizeWindowHint);
ui->stackedWidget->resize(qt_screen->deviceWidth().qt_screen->deviceHeight());
ui->stackedWidget->setCurrentWidget(ui->stackedWidgetPageMain);
initPageMain();
touch=new Touch();
powerButton=new PowerButton();
auxButton=new AuxButton();
usbOtg=new UsbOtg();
battery=new Battery();
panel=new Panel();
lan=new Lan();
hPattern= new HPattern();
lodLog=new LodLog();
record=new Record();
led=new Led();
lightsensor=new LightSensor();
}
Did i miss to include anything?
By the way i'm sure that i had include Qscreen into it.
Is there any possibility that i might set wrong on my code?

QScreen class has changed notably from Qt4 and Qt5, actually so much, that it is considered new in Qt5.
Qt4 QScreen had a static method called QScreen::instance() which returned a QScreen instance pointer. Way back in Qt4, this pointer was taken from a global variable qt_screen, if I remember correctly.
All this has changed, so simply remove all code which access qt_screen and fix it by properly accessing public APIs.

Related

Application crashes when using global QNetworkCookieJar for multiple windows

I am new at Qt and using Qt5.5 to create a http client application which will load a window with a fixed URL. After login in this window the other windows of same site should get the same session. At this purpose I have used global pointer jar of QNetworkCookieJar class and implemented on the following code for every window
Window1::Window1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Window1)
{
ui->setupUi(this);
QUrl webURL("http://someURL");
ui->webView->show();
ui->webView->load(webURL);
ui->webView->page()->networkAccessManager()->setCookieJar(jar);
}
It solves the multiple authentication problem, but when I close any window and reopen it instantly it causes appcrash.
Please give me a suggestion on my problem. Thanks in advance.
From the Qt5 docs (http://doc.qt.io/qt-5/qnetworkaccessmanager.html#setCookieJar):
Note: QNetworkAccessManager takes ownership of the cookieJar object.
So the accessmanager will delete your jar instance. There might be your problem! I don't know enough about the webview/page/accessmanager to be sure about the lifetime of the manager, but it seems to be bound to the webview/your ui, so when it's closed+destroyed, your cookiejar will have gone, too.
As QNetworkCookieJar inherits from QObject, you might want to use a guarded QPointer<QNetworkCookieJar> jar instead of a simple QNetworkCookieJar* jar variable. Then, you will notice that your guarded pointer becomes null after the first window is closed/destroyed. That would a) verify my claim from the last paragraph and b) guard you against stale pointer accesses in the future (in fact, your program would no longer crash).
Thanks ThorngardSO..
I have found a solution using your answer. Here is my solution,
if(jar.isNull()==true){
qDebug()<<"object null";
QPointer <QNetworkCookieJar> jar_new = new QNetworkCookieJar(0);
ui->webView->page()->networkAccessManager()->setCookieJar(jar_new);
jar=jar_new;
}
else
ui->webView->page()->networkAccessManager()->setCookieJar(jar);

Qt::WA_DeleteOnClose

I am learning Qt and trying some examples in the book "Foundations of Qt Development".
In the book, there is a section teaching Single Document Interface with an example creating a simple app like a notepad.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1[*] - %2").arg("unnamed").arg("SDI"));
connect(ui->docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));
createActions();
createMenu();
createToolbars();
statusBar()->showMessage("Done");
}
The book said that "Setting the windows attribute to Qt::WA_DeleteOnClose so that Qt takes care of deleting the window from memory as soon as it is closed.
How it works?
Because if i use setAttribute(Qt::WA_DeleteOnClose);, when I end the program, there is a Debug Assertion Failed warning:_BLOCK_TYPEIS_VALID(pHead->nBlockUse).
There is no problem if the setAttribute is removed.
Qt takes care of the deletion by itself if you set all the parenting right (if you create a new QObject/QWidget set the parent in the constructor). If the parent will be destructed, then the children will be too. In your main file, you can create the mainwindow on the stack, such that it will be destructed at the end of scope.
To call addToolbar you don't need this->, since it is a method of the class anyway.
The toolbar ptr should be a member to access it easily later on. But initialize it with nullptr (or NULL if you don't have c++11) in the initialization list of the constructor to know whether it is initialized or not.
The addToolBar call should work. A workaround would be to create a QToolBar on your own and add the pointer to the MainWindow using another addToolBar overload.

QWidget winId crashing if called from constructor

I'm writing a program, where i callQWidget::winId()in the constructor:
debug_window::debug_window(QWidget *parent) :
QDialog(parent),
ui(new Ui::debug_window),
hk(NULL)
{
this->ui->setupUi(this);
this->hk = new TestClass(this, this->winId())
}
But this will cause my program to crash, even before a window is created. I already figured out that the call of winId causes the crash, probably because at this time there is no window existing. (correct me if I'm wrong).
Sadly, there is no signal "windowCreated()" or something similar. So is there any way to find out that the window was created, or how to in general solve this problem?
Thanks for your help.
Create signal and emit him in the end of constructor.

QGLWidget crashes when added to layout

I am trying to adapt the opengl Es example "Hello GL" featured here - http://qt-project.org/doc/qt-4.8/opengl-hellogl-es.html. I am basically looking for a simple way to get a 3D graphics rendering window into a form made in Qt creator.
The first thing I tried:
Grid layout is a layout I created in Qt Creator.
#include <QProcess>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QTimer>
#include "glwidget.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
GLWidget *glwidget = new GLWidget(); // This is mandatory. No problems here.
QTimer *timer = new QTimer(this); // Need this for the example to work.
timer->setInterval(10); // Also necessary.
ui->gridLayout->addWidget(glwidget);
Which compiles, but then promptly crashes with a segmentation fault.
ui->gridLayout->addWidget(new GLWidget);
Segfaults the same way.
The debugger points me toward line 104 of qgridlayout.h:
inline void addWidget(QWidget *w) { QLayout::addWidget(w); }
Not sure what to make of that. Perhaps the QGLWidget wants to do something before I call ui->setupUi(this)? Perhaps it can't add the widget to the layout for some reason?
Of course if I comment out the line where I am added the widget, the program works flawlessly.
Any ideas for what's going on here?
Edit: I have fixed this. It was problem with order of operations - I called updateui too quickly.
The setupUi function in the generated form class initializes all variables of the form class, so using the variables before the form is initialized is undefined behaviour, since the variables contain garbage.
So the solution is to call:
ui->setupUi(this)
Before any call that uses variables in the ui object.
I'm not sure, but I think the problem is with the following line:
ui->gridLayout->addWidget(GLWidget);
I think, you should write it as follows:
ui->gridLayout->addWidget(glwidget);
// declare glwidget as member of your class
GLWidget *glwidget;
//in constructor use
glwidget = new GLWidget();

Fullscreen for QDialog from within MainWindow only working sometimes

(Testing with C++ on Qt 4.8 and Ubuntu 12.10 unity)
I've got a main window which displays a QDialog. When I put the Dialog window full-screen it does not seem to always work even though it seems to be a proper window.
Meaning, the window can appear full-screen, though only sometimes.
Anyone got an idea? I know Qt states it might not work for all X environments, but it can't be that bad, can it?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDialog* d = new QDialog();
d->setModal(false);
d->show();
qDebug() << d->isWindow();
// works most of the times, though not always:
// d->showFullScreen();
// sometimes works, sometimes it doesn't:
QTimer::singleShot(2000, d, SLOT(showFullScreen()));
}
DomTomCat here at stackoverflow showed a way to detect Ubuntu and the QDialog problem is related to a bug in Metacity (Ubuntu's default window manager).
Metacity doesn't manage the same way dialogs and main windows and it has to be tricked making it think that a QDialog is a normal window. In order to do so using QDialog class it's window flags have to be changed.
Instead of doing all the steps DomTomCat says, you can detect the session and then just
//example inside the QDialog
this->setWindowFlags(Qt::Window);
this->showFullScreen();
The bug was reported (and ignored) before but as far as I know this is the first simple workaround.
https://bugreports.qt.io/browse/QTBUG-16034
https://git.gnome.org/browse/metacity/tree/src/core/window.c#n6326
Ubuntu can also use compiz. This can be seen at:
grep DefaultProvider-windowmanager /usr/share/gnome-session/sessions/*
Best regards,
Iker De Echaniz.
I came to a method, which works. I don't know why it works compared to just calling showFullScreen(). I guess this is not the perfect and clean solution. This can surely be adapted to other environmental variables and X sessions.
QDialog* d = new QDialog();
d->setModal(false);
d->show();
const QString session = QString(getenv("DESKTOP_SESSION")).toLower();
QByteArray geometry;
if (session == "ubuntu") {
geometry = _d->saveGeometry();
d->setFixedSize(qApp->desktop()->size());
d->setWindowFlags(Qt::FramelessWindowHint);
d->setWindowState( d->windowState() | Qt::WindowFullScreen);
d->show();
d->activateWindow();
} else {
d->showFullScreen();
}
For restoring from the fullscreen state, this has worked
if (session == "ubuntu") {
d->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
d->setMinimumSize(0,0);
d->restoreGeometry(geometry);
d->setWindowFlags(Qt::Dialog);
d->show();
d->activateWindow();
} else {
d->showNormal();
}