Qt 5 QPrinterInfo::availablePrinters() not listing printers dynamically - c++

I am updating the printer list using availablePrinters(). But it fails to list the new printer added while running application. It is working fine with Qt 4.
The code can be seen below:
#include <QCoreApplication>
#include <QtPrintSupport/QPrinterInfo>
#include <QThread>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while (1) {
QThread::msleep(3000);
qDebug()<<"List of printers";
QList<QPrinterInfo> printerList=QPrinterInfo::availablePrinters();
foreach (QPrinterInfo printerInfo, printerList) {
qDebug()<<printerInfo.printerName();
}
}
return a.exec();
}

That was a bug with the existing Qt version, and It got fixed on the next version

Related

How to list /dev/sda usb storages mounted with a combobox

I'm looking for the way to show up usb storage path when they are plugged in, the path must be shown in a combobox (in a gui that I'm designing with qt creator (qt 5.9)). I have been searching how to do it but I have not found anything. What I want it's something like:
https://catenarios2.files.wordpress.com/2012/11/002.jpg
Could you please help me to carry on my project? I would be very grateful if you provide an example.
Thank you a lot
The basic idea is the same -- you launch Linux tool via QProcess and parse the result. Here is a simple sketch:
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
#include <usb.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess devList;
devList.start("lsblk", QStringList() << "-o" << "KNAME");
if (!devList.waitForStarted())
return false;
if (!devList.waitForFinished())
return false;
QString result = QString(devList.readAll());
qDebug() << result;
return a.exec();
}
You can use any other siutable command (quite easy to find them) and should improve parsing, of course, but generally it's all the same.
AFAIK, mount points could be obtained from /proc/mounts with something like...
#include <QCoreApplication>
#include <mntent.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct mntent *ent;
FILE *aFile;
aFile = setmntent("/proc/mounts", "r");
if (aFile == NULL) {
perror("setmntent");
exit(1);
}
while (NULL != (ent = getmntent(aFile))) {
printf("%s %s\n", ent->mnt_fsname, ent->mnt_dir);
}
endmntent(aFile);
return a.exec();
}
Better than cat launching or someting else, also taken from some snippet and should be improved.
And, finally, in case you'll need USD device info, it could be something like...
#include <QCoreApplication>
#include <usb.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct usb_bus *bus;
struct usb_device *dev;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
printf("Trying device %s/%s\n", bus->dirname, dev->filename);
printf("\tID_VENDOR = 0x%04x\n", dev->descriptor.idVendor);
printf("\tID_PRODUCT = 0x%04x\n", dev->descriptor.idProduct);
}
}
return a.exec();
}
This needs sudo apt-get libusb-dev + compiling with -lusb.
Not really much of Qt in the problem and more fundamental "coding" solutions possible, but hopefully that'll give you a push towards appropriate solution.

Using QChart in visual studio

I am trying to use the Qt QChart to plot a line graph. I am using visual Studio 2013 with Qt 5.8. I created a simple QApplication. When I paste QLineSeries *series it says QLineSeries is undefined. How can I fix this?
#include "QtGuiApplication2.h"
#include <QtWidgets/QApplication>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtGuiApplication2 w;
QLineSeries *series = new QLineSeries();
w.show();
return a.exec();
}
you need to link against QT5Charts under:
general properties
- Linker
- Input
- add the path: e.g. C:\Libraries\Qt\Qt5.7.0\msvc\lib\Qt5Chartsd.lib
I suppose, you don't forget to add the namespace?
using namespace QtCharts;
if you work with cmake, just add this to your cmake file:
find_package(Qt5Charts)
target_link_libraries(${targetName} Qt5::Charts)

How to create a directory using QT (QDir) in Linux?

I have been trying to create a directory in root directory of Linux. But as I am not much familiar with Linux platform I am unable to write the correct program in QT. Can you please have a look at my code and tell me where did I did mistake?
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QString>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir mDir;
QString mpath="/home/qtfile";
if (!mDir.exists(mpath))
{
mDir.mkpath(mpath);
qDebug() <<"Created";
}
else if (mDir.exists(mpath))
{
qDebug() <<"Already existed";
}
else
{
qDebug()<<"Directory could not be created";
}
return a.exec();
}
Thank you for your time and consideration
EDIT:- Thank you everyone. Now this problem is solved
This might be the issue of access rights #SamratLuitel is writing about in the comments.
Hence, you could try to give it a go in the proper home location, for example:
const QString& homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
QDir dir(homePath);
if (dir.mkdir("somedir"))
{
//success
}

FLTK draw pixmap gives segfault

I'm trying to draw a xpm file in a C++ program with FLTK.
Here's the code
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include "image.xpm"
#include <FL/Fl_Pixmap.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Image.H>
int main(int argc, char ** argv)
{
Fl_Window *window = new Fl_Window(800,650);
Fl_Pixmap pix(XFACE);
pix.draw(200,200);
window->end();
window->show(argc,argv);
return Fl::run();
}
XFACE is a valid xpm object inside "image.xpm"
But I'm getting a segmentation fault at the pix.draw() line.
What causes this?
/* Try this - this works for me, and I guess is what you meant! */
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Pixmap.H>
#include "image.xpm"
int main(int argc, char ** argv)
{
Fl_Window *window = new Fl_Window(800,650);
Fl_Box *image_box = new Fl_Box(5, 5, 790, 640);
Fl_Pixmap pix(XFACE);
window->end();
image_box->image(pix);
window->show(argc,argv);
return Fl::run();
}
/* end of file */
To be honest, that doesn't even look like valid fltk code; you are calling the draw() method directly, and AFAIK that is seldom valid in fltk.
You probably want to ask over on their mailing list - they are pretty responsive.
Also, did you look at the pixmap demo in the "test" folder of the tarball - see what it does, then copy it!

ReferenceError when using c++ and qml on Nokia N9

I want to integrate c++ and qml. However, my code works fine in simulator but not in Nokia N9 (Qt 4.7.4 harmattan_10.2011.34-1)
Here is my code
I pasted c++ and qml code here for your reference
#include <QtGui/QApplication>
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeContext>
#include <QtDeclarative/QDeclarativeEngine>
#include "qmlapplicationviewer.h"
#include "data.h"
#include "testfactory.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
TestFactory *testfactory = new TestFactory();
QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
QDeclarativeContext *context = viewer->rootContext();
context->setContextProperty("testfactory", testfactory);
viewer->setOrientation(QmlApplicationViewer::ScreenOrientationLockPortrait);
viewer->setMainQmlFile(QLatin1String("qml/main.qml"));
viewer->showExpanded();
testfactory->intilize();
return app->exec();
}
Button {
id: startButton
text: qsTr("Start")
onClicked: {
mainview.state = "START"
testfactory.startMeasurement()
}
}
The wield part is that the code works on simulator but the device.
The error I get is ReferenceError: Can't find variable: testfactory
Any one knows what the reason is?
Based on comments from irc qt-qml, one solution is to just use
QmlApplicationViewer *viewer = new QmlApplicationViewer();
instead of
QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
Then code works.