Qt: Handle 'Save Print Output As' dialog with QPainter::begin() - qt

here is a minimal reproducable example of my problem:
#include <QCoreApplication>
#include <QtPrintSupport/QPrinter>
#include <QPainter>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QPrinter printer;
QPainter painter;
// this will open a file dialog to save a pdf file
if (!painter.begin(&printer)) {
qDebug() << "how to get the difference between 'Dialog Canceled' and 'File in use' here?";
}
else {
//do the painting stuf...
}
return a.exec();
}
My question is - as already mentioned in the code - how do I know if the dialog is canceled by the user or the pdf file is just in use (for example opened in Adobe Reader)?
I already tried to check the printer.outputFormat() and the printer.docName() but they are both empty.

Related

The line edit widget shows nothing

I've recently started learning Qt and I'm a beginner of it now. So as first example for myself I wrote the following simple example.
The example is named Calculator. It now only has two buttons an a line edit. It's here:
:
My Calculator.h is this:
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<QDialog>
#include "ui_Calculator.h"
class Calculator : public QDialog, public Ui::Calculator
{
Q_OBJECT
public:
Calculator(QWidget* parent = 0);
private slots:
void myslot();
};
#endif // CALCULATOR_H
And the Calculator.cpp is this:
#include <QtWidgets>
#include "calculator.h"
Calculator::Calculator(QWidget *parent)
:QDialog(parent)
{
setupUi(this);
connect(oneButton,SIGNAL(clicked(bool)), this, SLOT(myslot()));
}
void Calculator::myslot(){
lineEdit -> setText("1");
}
And this is the main.cpp:
#include <QApplication>
#include <QDialog>
#include "ui_Calculator.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Ui::Calculator ui;
QDialog* dialog = new QDialog;
ui.setupUi(dialog);
dialog -> show();
return app.exec();
}
The program runs fine without any error. But when I click on 1 button, nothing will be printed/shown in the line edit. Why please?
And what part of my program should I change to solve the issue please?
You are setting up the wrong class in your main.
You should use your custom Calculator class and not QDialog.
setupUi only initializes your elements but your code in Calculator never gets called. Your main should look like this:
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Calculator calc; //using your Calculator class.
calc.show();
return app.exec();
}
And don't include ui_calculator.h but calculator.h

QWebEngineView RAM problems (all memory is taken by it in a minute)

Hello so i have a BIG problem with QWebViewEngine so far. Because all i did was created a QWebEngineView and said .load(QUrl("http://google.com")) and then .showFullScreen(). On start the application took about 130MB of RAM. When i pressed feel lucky on google and the page loaded suddenly the RAM started to climbing by 200mb each second and it stopped when there was no more free RAM.
Anyone had this problem, or experience with QWebEngineView.
I know its Chormium, but it seems to me as if it wasnt working correctly.
Any suggestions how to correct this?
Edited 14/08/2015 14:12
here is the code(note that most of it is commented):
#include "mainwindow.h"
#include <QtWebEngineWidgets/QtWebEngineWidgets>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QScopedPointer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
view = new QWebEngineView();
manager = new QNetworkAccessManager();
settings = new QSettings(":/settings.ini",QSettings::IniFormat);
// connect(view,SIGNAL(loadFinished(bool)),this,SLOT(CheckPage()));
// connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(connection(QNetworkReply*)));
// errorOpen=false;
settings->beginGroup("URL");
myUrl = settings->value("curUrl").toString();
settings->endGroup();
// view->load(myUrl);
view->load(QUrl("http://google.com"));
view->showFullScreen();
settings->deleteLater();
}
MainWindow::~MainWindow()
{
// delete view;
// delete manager;
}
I can't reproduce under qt5-mac #5.4.2_1 from macports on OS X 10.9:
//main.cpp
#include <QtWebEngineWidgets>
#include <QApplication>
int main(int argc, char ** argv)
{
QApplication a(argc, argv);
QWebEngineView view;
view.load(QUrl("http://google.com"));
view.showFullScreen();
return a.exec();
}
# chromium-32008560.pro
QT += webenginewidgets
TARGET = chromium-32008560
TEMPLATE = app
SOURCES += main.cpp

Qt is successfully creating a file but not writing to it with QTextStream

Hey I'm trying to mess around with Qt and for some reason the following code will create the desired text file, but never writes anything to it. Am I doing something wrong? I believe I've copied the example in the documentation pretty accurately.
qDebug() << output
works as expected, but even though the file is created, nothing is ever written to it.
#include <QCoreApplication>
#include <QtDebug>
#include <QString>
#include <QDateTime>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString filename = "";
filename.append(QString::number(QDateTime::currentMSecsSinceEpoch()));
filename.append(".txt");
QFile file(filename);
file.open(QIODevice::WriteOnly);
QTextStream out(&file);
QString output = "TEST";
qDebug() << output;
out << output;
return a.exec();
}
The data does not get written to disk immediately: It sits in a buffer until it's flushed.
Close the file after you've finished writing.
(In my experience, the file is closed anyway when you quit the program, but it's good practice to do this explicitly)

Problems with function QTimer::singleShot

I am trying to record sound by QAudioInput. According to the doc in this website QAudioInput. But when I ran, it exported an empty-raw file. After checking, It seems like the function QTimer::singleShot didn't working ( I added statement qWarning << "Done" in void stopRecording() and It didn't display "Done" so I thought it had some mistake in QTimer::singleShot function ).
This is my code used to check function QTimer::singleShot
----Check.pro----
QT += core
QT -= gui
TARGET = Check
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += test.h
-----test.h------
#ifndef TEST_H
#define TEST_H
#include <QCoreApplication>
#include <QTimer>
#include <iostream>
#include <QObject>
#include <test.h>
#include <QDebug>
using namespace std;
class Object: public QObject {
Q_OBJECT
private slots:
void func() { cout << "Hello"; }
};
#endif // TEST_H
----main.cpp----
#include <QCoreApplication>
#include <QTimer>
#include <iostream>
#include <QObject>
#include <test.h>
#include <QDebug>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Object *o = new Object;
QTimer::singleShot(10000, o, SLOT(func()));
return 0;
}
And this code doesn't working, too. Can anyone explain me? I am newbie at Qt-programming.
Your program exits right after it's set the timer - it has no time to fire.
For the timer to work, you need an event loop running. Without the event loop, no events get processed.
Change the last line of your main to
return a.exec();
Also change your test slot by adding << std::endl or flush std::cout otherwise you might see no output on the console.
Your program should then work as expected (except it won't ever finish since nothing will cause the event loop to stop - just interrupt it).

Error Occured while designing Dialog in Qt

The program name is GoToCell in the form I created a label and two push buttons and I wrote the code in main.cpp as follows:
#include <QtGui/QApplication>
#include<QDialog>
#include "ui_GoToCell.h"
#include "GoToCell.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
Ui::GoToCell ui;
QDialog *dialog=new QDialog;
ui.setupUi(dialog);
dialog->show();
return a.exec();
}
While running it I'm getting the following errors:
GoToCell is not a member of ui
What should I do?
I think you have misspelled object name in objectName property in the GoToCell.ui form.
Change it to GoToCell. It will then execute.

Resources