Points of misunderstanding
1. what is the purpose of this line ?
QApplication::setAttribute(Qt::AA_EnableHighDpiSca ling);
It is unnecessary. All it does is to tell Qt to use the full capability of high DPI (dots per inch) monitors (like 4K monitors) when scaling pixmaps to fit their windows.
2. do I have to implement it when btscanner QDilog is added to main -> tab ?
This is a two part question actually. No, you do not need to enable the high dpi scaling, but you can leave that line there with no harm if you want.
The second part of the question is adding the DeviceDiscoveryDialog to the main window's tab widget. This probably will not work. Dialogs are meant to be stand-alone top level windows, not embedded inside a window owned by another window. So you can't just drop it down there and expect it to work properly.
. I have limited understanding of "connect" - I know how to implement it in simple ui control.
I do understand how most of the btscanner slot/signals works.
4. What is the purpose of this line of code AND how to implement it - in main window or tab ?
QObject::connect(&d, SIGNAL(accepted()), &app, SLOT(quit()));
My guess it allows to process "quit" button and quits entire btscanner application.
I suggest you read the section of Qt's documentation on signals and slots, which has an extensive description of how the process works. Google can tell you where that section is if you ask it about "Qt signals and slots".
That particular connect() call does say that when the user clicks the "OK" button on the DeviceDiscoveryDialog widget (and the widget emits the "accepted() signal as a result), the application should quit. Another reason why you can't just drop the dialog into another app without modification.
Personally, I think you might be a in little bit over your head in this thing. However, the Qt btscanner app has complete source code and everything is implemented in just two QDialog-based classes, DeviceDiscoveryDialog and ServiceDiscoveryDialog.
So, starting with this source code, the first thing you do is to change DeviceDiscoveryDialog so that it inherits from QWidget instead of QDialog. This will enable you to add it to a tab in your MainWindow class.
Next, remove the connect() with the accepted() signal. QWidget does not have such a signal, only QDialog. You will have to implement a QMenu item on your main window to exit the program instead.
You should not have to change the ServiceDiscoveryDialog class. This is meant to pop up as a dialog whenever an item is selected from the device list.
So simply changing the DeviceDiscoveryDialog class to a QWidget-based class and adding it to your tab widget should get you started with a semi-working app.
// device.h
#include <QWidget> // <<<
QT_FORWARD_DECLARE_CLASS(QBluetoothDeviceDiscoveryAgent)
QT_FORWARD_DECLARE_CLASS(QBluetoothDeviceInfo)
QT_USE_NAMESPACE
class DeviceDiscoveryWidget
: public QWidget // <<<
// all the rest is the same
// device.h
#include <QWidget> // <<<
QT_FORWARD_DECLARE_CLASS(QBluetoothDeviceDiscoveryAgent)
QT_FORWARD_DECLARE_CLASS(QBluetoothDeviceInfo)
QT_USE_NAMESPACE
class DeviceDiscoveryWidget : public QWidget // <<<
// all the rest is the same
To copy to clipboard, switch view to plain text mode
// device .cpp
DeviceDiscoveryWidget
::DeviceDiscoveryWidget(QWidget *parent
) // <<<: QWidget(parent
), localDevice
(new QBluetoothLocalDevice
),
// <<< ui(new Ui_DeviceDiscovery)
{
// all the rest is the same
// device .cpp
DeviceDiscoveryWidget::DeviceDiscoveryWidget(QWidget *parent) // <<<
: QWidget(parent), localDevice(new QBluetoothLocalDevice), // <<<
ui(new Ui_DeviceDiscovery)
{
// all the rest is the same
To copy to clipboard, switch view to plain text mode
// device.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DeviceDiscovery</class>
<widget class="QWidget" name="DeviceDiscovery">
<property name="geometry">
// device.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DeviceDiscovery</class>
<widget class="QWidget" name="DeviceDiscovery">
<property name="geometry">
To copy to clipboard, switch view to plain text mode
// MainWindow constructor:
DeviceDiscoveryWidget * ddw = new DeviceDiscoveryWidget(); // Note the name change.
tabWidget->addTab( ddw, "Device Discovery" );
// MainWindow constructor:
DeviceDiscoveryWidget * ddw = new DeviceDiscoveryWidget(); // Note the name change.
tabWidget->addTab( ddw, "Device Discovery" );
To copy to clipboard, switch view to plain text mode
You can edit "device.ui" using an ordinary text editor to change the QDialog to QWidget in the 4th line.
Your main.cpp file (where you create you MainWindow) does not change at all.
At some point you will probably want to make the DeviceDiscoveryWidget pointer a member of your MainWindow class, because it is likely that you will want to add signals or other features to the class, and you'll need to be able to access from your MainWindow class.
Bookmarks