Using QChart in visual studio - c++

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)

Related

How to create QT Login Page bedore Mainwindow?

My Qt windows application is ready, but when the application opens, I want the login dialog to be opened, how can I do this? I'm new to Qt and C++. It would be great if it was descriptive.
You have many ways to achieve that... QDialog is a nice way. Here is a short sample using QInputDialog.
One solution could be to add this code in your main.cpp file, and to load the mainwindow only if the credentials are ok.
#include "gmainwindow.h"
#include <QApplication>
#include <QInputDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GMainWindow w;
QString login = QInputDialog::getText(NULL, "Login","Name ?",QLineEdit::Normal);
if (login == "USER")
{
w.show();
}
else
{
//display an error message
return a.quit();
}
return a.exec();
}
Of course you may want to put an encrypted password and other things, but the idea will be more or less the same.

how to translate key shortcut

I cannot force QKeySequence::toString() to return translated shortcut representation despite the fact that it documentation suggests it should work. The docs say: "The strings, "Ctrl", "Shift", etc. are translated using QObject::tr() in the "QShortcut" context." but I am not completely sure what it means by shortcut context. I am probably doing something wrong...
Here is my example. To make it work, I need to copy qtbase_es.qm from Qt installation directory to my project build directory. When the translation is correctly loaded, the action in the menu correctly shows "Action Control+Intro" which is Spanish translation of the shortcut for "Action Ctrl+Enter". But the tooltip on the main window is still "Action (Ctrl+Enter)". I would expect it to be "Action (Control+Intro)", like in the menu. What am I doing wrong?
#include <QAction>
#include <QApplication>
#include <QDebug>
#include <QMainWindow>
#include <QMenuBar>
#include <QTranslator>
int main(int argc, char *argv[])
{
QTranslator spanish;
qDebug() << spanish.load("qtbase_es.qm"); // should return true if correctly loaded
QApplication a(argc, argv);
QApplication::installTranslator(&spanish);
QMainWindow w;
auto menu = new QMenu("Menu");
auto action = menu->addAction("Action");
action->setShortcutContext(Qt::ApplicationShortcut);
action->setShortcut(Qt::CTRL | Qt::Key_Enter);
w.menuBar()->addMenu(menu);
w.show();
QApplication::processEvents(); // I also tried this line but it is useless...
w.setToolTip(QString("%1 (%2)").arg(action->text(), action->shortcut().toString()));
qDebug() << action->shortcut().toString(); // WRONG: returns Ctrl+Enter but I expect Control+Intro
return a.exec();
}
The QShortcut::toString has a SequenceFormat parameter, defaulted to ProtableText. The documentation of the format states, that portable format is intended for e.g. writing to a file.
The native format is intended for displaying to the user, and only this format performs translations.
Try:
qDebug() << action->shortcut().toString(QKeySequence::NativeText);

Writing commands on linux terminal using Qt

I'm trying to get Qt creator to print a user input by using a push button on an UI into the terminal. As of now, the code is executable on the terminal via human input. Here is the code:
void MainWindow::on_pushButton_clicked()
{
QProcess::execute("/catkin_ws/devel/lib/submodbus");
system("cd catkin_ws/devel/lib/submodbus");
system("./submodbus_node");
}
Current output when using the code
Output via human input
The versions i'm running on are:
-Ubuntu 16.04
-QT Creator 3.5.1
system can't change the current directory globally. but could use like this:
system("cd /catkin_ws/devel/lib/submodbus && ./submodbus_node");
or using QProcess::setProgram with QProcess::setWorkingDirectory
QProcess p;
p.setProgram("submodbus_node");
//p.setArguments(QStringList()<<args); // if you need
p.setWorkingDirectory("/catkin_ws/devel/lib/submodbus");
p.start();
or QDir::setCurrent
QDir::setCurrent("/catkin_ws/devel/lib/submodbus");
QProcess::startDetached("submodbus_node");
Test demo, create three files in the parent directory:
#include <QApplication>
#include <QProcess>
#include <QDir>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
system("cd ../ && touch test1.txt");
QProcess p;
p.setProgram("touch");
p.setArguments(QStringList()<<"test2.txt");
p.setWorkingDirectory("../");
p.start();
QDir::setCurrent("../");
QProcess::startDetached("touch test3.txt");
return a.exec();
}

Qt 5 QPrinterInfo::availablePrinters() not listing printers dynamically

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

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.