I want to create a PDF with Qt. This is my code:
#include <QPdfWriter>
#include <QTextDocument>
int main() {
QPdfWriter pdfWriter{R"(C:\TEMP\test.pdf)"};
QTextDocument document;
document.setPlainText("Hello world");
document.print(&pdfWriter);
}
It crashes on the last line with
QPaintDevice: Cannot destroy paint device that is being painted
What did I do wrong?
You didn't create a QApplication. Change the code to
#include <QApplication>
#include <QPdfWriter>
#include <QTextDocument>
int main(int argc, char *argv[]) {
QApplication app{argc, argv};
QPdfWriter pdfWriter{R"(C:\TEMP\test.pdf)"};
QTextDocument document;
document.setPlainText("Hello world");
document.print(&pdfWriter);
}
and it will create the PDF. Running app.exec() is not required.
Related
I'd like to show and then close a dialog after 5 seconds. The dialog needs to be automatically resized (horizontally and vertically) based on the content of a label. Here is my code:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(¬ify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.adjustSize();
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
It does not not expand the dialog based on the label size. Here is how it looks:
How can I fix it? (Please also let me know if there is better way of doing this.)
I am using Qt5 in Linux.
Since you have not used a QLayout the QLabel will be displayed as large as you can, a possible request is to change the size of QDialog to the recommended size of QLabel with sizeHint():
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(¬ify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.resize(lbl->sizeHint());
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
The other possible solution is to use a QLayout:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
void notify (int intTime=1000)
{
QDialog notify;
QVBoxLayout *lay = new QVBoxLayout(¬ify);
//notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel;
lay->addWidget(lbl);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
I'm trying to setup translations for a qt app but it seems that the translations files are loaded after the window because the only translated text are the ones in messages box
This is the code:
#include <QApplication>
#include <QTranslator>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QCoreApplication::setOrganizationName("Lebbadi");
QCoreApplication::setOrganizationDomain("lebbadi.fr");
QCoreApplication::setApplicationName("zNavigo");
QTranslator translator;
if(translator.load(QLocale(QLocale::French), "app", "_", ":/translations"))
app.installTranslator(&translator);
else
exit(-1);
MainWindow window;
window.show();
return app.exec();
}
Thanks
The exact crash when debugging is:
The inferior stopped because it triggered an exception.
Stopped in thread 0 by: Exception at 0x7fed96c6cda, code: 0x0000005: read access violation at: 0x0, flags=0x0.
The exception then points to this line in the below code:
if(QOpenGLContext::currentContext()->isValid())
The below code is enough to reproduce the exception for me.
Subclassing the QOpenGLWidget class and making the subclass call initializeGL() once before attempting to access context does not fix problem.
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLContext>
#include <QDebug>
void initialize(QOpenGLWidget * renderArea)
{
renderArea->makeCurrent();
if(QOpenGLContext::currentContext()->isValid())
{
qInfo() << "Valid.";
}
}
int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setVersion(3,3);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
QApplication a(argc, argv);
QOpenGLWidget * glw = new QOpenGLWidget;
initialize(glw);
return a.exec();
}
I now realize the answer: You have to do all such opengl initialization AFTER the event loop starts.
A fixed code where the "MainWindow" class does all opengl initialization on receiving signal "onEventLoopStarted":
#include "mainwindow.h"
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLContext>
#include <QDebug>
#include <QTimer>
int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setVersion(3,3);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
QApplication a(argc, argv);
MainWindow w;
w.resize(512, 512);
w.show();
QTimer::singleShot(0, &w, SLOT(onEventLoopStarted()));
return a.exec();
//Window receives event and begins to initialize.
}
Let's say we have a barebones QWebView:
#include <QApplication>
#include <QWebView>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QWebView view;
view.show();
view.setUrl(QUrl("http://google.com"));
return app.exec();
}
How can I display a graphic overlay, preferably fullscreen with transparency and minimal animation (like timer/beachball/etc.) from when the page starts loading till it's finished? Should also be triggered when url changes from within the QWebView.
You can use the LoadingOverlay class provided in this answer to draw an overlay over any QWidget. In your case, show the overlay on top of the QWebView when the signal loadStarted is triggered and hide it, when the signal loadFinished is triggered.
The following code should get you started. I put the code from the linked answer into overlay.h, the subclass of QWebView which handles the showing/hiding of the overlay is in webview.h:
webview.h
#include "overlay.h"
#include <QWebView>
class WebView : public QWebView
{
Q_OBJECT
public:
WebView(QWidget * parent) : QWebView(parent)
{
overlay = new LoadingOverlay(parent);
connect(this,SIGNAL(loadFinished(bool)),this,SLOT(hideOverlay()));
connect(this,SIGNAL(loadStarted()),this,SLOT(showOverlay()));
}
~WebView()
{
}
public slots:
void showOverlay()
{
overlay->show();
}
void hideOverlay()
{
overlay->hide();
}
private:
LoadingOverlay* overlay;
};
main.cpp
#include <QApplication>
#include "overlay.h"
#include "webview.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ContainerWidget base;
Webview w(&base);
base.show();
w.load(QUrl("http://google.com"));
return a.exec();
}
I want to alternate the colors of a QComboBox. In Windows I have no problem using the view().setAlternatingRowColors(true) function. In Linux and Mac it looks like impossible. I tried also using style sheet (see following code) but I had the same kind of results (all rows with the same background color). Can you explain me what is my error?
#include <QtGui/QApplication>
#include <QComboBox>
#include <QAbstractItemView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyleSheet("QComboBox QAbstractItemView{qproperty-alternatingRowColors: true;alternate-background-color: blue;background: red;}");
QComboBox b;
b.addItem("MM_NONE");
b.addItem("MM_VERT");
b.addItem("MM_FACE");
b.addItem("MM_EDGE");
bool tt = false;
tt = b.view()->alternatingRowColors();
b.show();
return a.exec();
}
At least on my box it appears that QPalette::Base and QPalette::AlternateBase are the same color :) Changing QPalette::AlternateBase to some other color makes this code work fine:
#include <QtGui/QApplication>
#include <QComboBox>
#include <QAbstractItemView>
#include <QPalette>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QComboBox b;
b.view()->setAlternatingRowColors(true);
QPalette p = b.palette();
p.setColor(QPalette::AlternateBase, Qt::red);
b.setPalette(p);
b.addItem("MM_NONE");
b.addItem("MM_VERT");
b.addItem("MM_FACE");
b.addItem("MM_EDGE");
b.show();
return a.exec();
}