Simplest Qt Dialog - c++

I have a C++ function that checks if Bluetooth is activated. I want to display a simple dialog telling the user to activate his Bluetooth and try again. As I have a QML interface this can be done through C++ or QML.

You can use the built-in information message box:
#include <QApplication>
#include <QDebug>
#include <QMessageBox>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QMessageBox::information(0, "Try Again", "Please try to activate your Bluetooth again.");
}

Qt Components have some dialogs out of box:
http://doc.qt.nokia.com/qt-components-symbian-1.0/qml-querydialog.html

Related

In Qt main function, how does QApplication learn about Mainwindow?

Looking at a simplest Qt Widget sample application that you can find from almost every Qt tutorial:
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();
return a.exec();
}
There is one thing puzzles me. There are two major variables a and w here. a.exec() starts Qt's main loop, which suppose to interact with the main GUI component w. However, both of them live on stack and I don't see any code pass w somehow to a. So how does a be aware of the existence of w?
Does the constructor of w initializes a static data structure that a can access to check the top-level widgets?
Qt preprocess your code and build the real c++ code before compiling, its at this moment QApplication wrap all Q object in the main.cpp file and build the rest of the code from it.

Can input audio be processed by more apps at once with Qt?

I would like to use the QAudioRecorder to record audio from microphone. My question is, should there be an ongoing Skype call or another application would try to use the microphone, would this result in some error or would both that app and my app receive same audio input data?
Similarly, can I use the QCamera and QMediaRecorder to access webcam that is being used already?
Yes, that will work just fine, there won't be any error. Both applications will receive the same audio input data. I tested to record the same thing at the same time using Windows' voice recorder program and a simple Qt program built with the following code, and the result was that both output files were recorded the sound correctly:
#include <QApplication>
#include <QAudioRecorder>
#include <QPushButton>
#include <QUrl>
int main(int argc, char **argv){
QApplication app(argc, argv);
QAudioRecorder *audioRecorder = new QAudioRecorder;
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/amr");
audioSettings.setQuality(QMultimedia::HighQuality);
audioRecorder->setEncodingSettings(audioSettings);
audioRecorder->setOutputLocation(QUrl::fromLocalFile("C:\\Users\\dduck\\Desktop\\test.amr"));
QPushButton b("Start");
QObject::connect(&b, &QPushButton::clicked, [&](){
if(b.text() == "Start"){
audioRecorder->record();
b.setText("Stop");
}
else{
audioRecorder->stop();
app.quit();
}
});
b.show();
return app.exec();
}
I assume it would also work if you run that program at the same time as Skype.
So yes, input audio can be processed by a Qt program at the same time as it is processed by other programs.

Selecting from all available serial ports using Qt GUI

I could not find a conclusive answer to my issue so I decided to post my first question on this site. I'm fairly new to programming and have been using Qt for a couple of months now.
My code communicates with a microcontroller via serial ports, however the available port differs from pc to pc. I'm displaying the number of ports available with the code;
qDebug() << "Number of serial ports:" << QSerialPortInfo::availablePorts().count();
My question is: how can I display the name of all the available ports eg "COM 10, 17. 22, etc" and then show them in my GUI. What I eventually hope to do is have a combo box that can be dynamically populated with the available ports, I have one that switches between a couple ports at the moment but these are fixed ports corresponding to particular computers.
Try something like this:
#include <QApplication>
#include <QWindow>
#include <QSerialPortInfo>
#include <QComboBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.resize(200,200);
w.show();
QComboBox box(&w);
Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) {
box.addItem(port.portName());
}
box.move(100 - box.width() / 2,100 - box.height() / 2);
box.show();
return a.exec();
}
The code is pretty self-explanatory.
Found a relevant answer on qt centre, example code;
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
ui->comboBox->addItem(serialPortInfo.portName());
}

QT HTML5 with Menu Bar like real program

I'm working on an QT HTML5 application and I was wondering how i could add an top menu just like normal program ( with the default file, tools, help... options ).
I think that I've to change something in the html5applicationviewer.cpp, but I've got 0 knowledge of that ( I'm learning this... )
Even if you could point me a little bit in the right direction, I'm grateful. I've searched around, but found absolutely nothing about this topic ( but maybe i didn't search right... )
If you need more info, please ask.
Simplest way to add normal "desktop"-style menus to a Qt app is to use QMainWindow which has good support for menus.
Here's something to get you started. First I created the default HTML5 Qt application with Qt Creator (SDK version 5.2.1). Then I edited the main.cpp and added some lines. Result is below, original lines without comment and all added lines with comment.
#include <QApplication>
#include <QMainWindow> // added
#include <QMenuBar> // added
#include <QMenu> // added
#include <QAction> // added
#include "html5applicationviewer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow w; // important, viewer is in stack so w must be before it!
Html5ApplicationViewer viewer;
w.setCentralWidget(&viewer); // set viewer as the central widget
QMenu *fileMenu = w.menuBar()->addMenu("&File"); // create file menu
QAction *exitAction = fileMenu->addAction("&Exit"); // create exit action
QObject::connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); // make the action do something
viewer.setOrientation(Html5ApplicationViewer::ScreenOrientationAuto);
//viewer.showExpanded(); // will be shown by main window
viewer.loadFile(QLatin1String("html/index.html"));
w.show(); // show main window
return app.exec();
}

QWebkit display local webpage

I'm working at a browser-like project based on QtWebKit.
It can display any webpage as good as any other browser can, but I can't make display local html documents!
I am using a QtWebView in a QMainWindow and I'm loading pages with
view->show();
Can anyone tell me what is wrong?
Thank you wery much!!!
I don't see anything different than loading a normal web page, but anyway this is taken from my answer here:
#include <QtGui/QApplication>
#include <QWebView>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QWebView view;
view.setUrl(QUrl("file:///home/luca/mypage.html"));
view.show();
return a.exec();
}