I want to use web browser to open my app and pass some data.
I can using urlApp:// open app by web browser, but how to pass data?
I tried urlApp://test there is not received to HandleURL::handleURL.
Platform
Qt 6.3.0
macOS 12.4
main.cpp register app url.
int main(int argc, char *argv[])
{
....
HandleURL *URLHandler = new HandleURL();
QDesktopServices::setUrlHandler("urlApp", URLHandler, "handleURL");
....
engine.rootContext()->setContextProperty("URLHandler", URLHandler);
....
}
HadleUrl
#ifndef HANDLEURL_H
#define HANDLEURL_H
#include <QObject>
#include <QDesktopServices>
#include <QUrl>
class HandleURL : public QObject
{
Q_OBJECT
public:
explicit HandleURL(){}
signals:
void incomingURL(QString path);
public slots:
void handleURL(const QUrl &url){
emit incomingURL(url.toString());
}
};
#endif // HANDLEURL_H
info.plist add CFBundleURLTypes
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>urlApp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>urlApp</string>
</array>
</dict>
</array>
Related
My child widget does not get keyPressEvents, while if I put the same widget as top level window, it does. I try to set it get focus, but it has no effect on this. Code is below, showing what I try to get to work.
#include <QApplication>
#include <QKeyEvent>
#include <QLCDNumber>
#include <QLabel>
#include <QVBoxLayout>
class DigitSummer: public QLCDNumber {
Q_OBJECT
public:
DigitSummer(QWidget *parent = nullptr) : QLCDNumber(parent) {
}
protected:
void keyPressEvent(QKeyEvent *event) override {
display(intValue() + event->text().toInt());
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
#if 1 // this version does not work, number does not increase
QWidget widget;
widget.setLayout(new QVBoxLayout());
widget.layout()->addWidget(new QLabel("Press digits!"));
DigitSummer summer; // in stack: must be after widget to avoid child delete
widget.layout()->addWidget(&summer);
widget.setFocusProxy(&summer); // I notice no effect!
widget.show();
#else // this version works, number grows with keypresseas
DigitSummer summer;
summer.show();
#endif
return a.exec();
}
#include "main.moc"
And for completenes, .pro file for the same:
QT += core gui widgets
TARGET = QtMCVE
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11
QMAKE_CXXFLAGS += -Wall -Wextra
SOURCES += main.cpp
How to fix the widget to receive key events?
This related question suggests installing event filter, but I don't want to do that, there must be a self-contained way to fix the widget itself.
I think you need to set the focus policy for the widget before it will accept keyboard input. In your ctor try...
setFocusPolicy(Qt::StrongFocus);
Having said that, I'm really not sure why the behaviour would differ for top-level and non-top-level widgets.
Working version of the question code:
#include <QApplication>
#include <QKeyEvent>
#include <QLCDNumber>
#include <QLabel>
#include <QVBoxLayout>
class DigitSummer: public QLCDNumber {
Q_OBJECT
public:
DigitSummer(QWidget *parent = nullptr) : QLCDNumber(parent) {
setFocusPolicy(Qt::StrongFocus);
}
protected:
void keyPressEvent(QKeyEvent *event) override {
display(intValue() + event->text().toInt());
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget widget;
widget.setLayout(new QVBoxLayout());
widget.layout()->addWidget(new QLabel("Press digits!"));
widget.layout()->addWidget(new DigitSummer);
widget.show();
return a.exec();
}
#include "main.moc"
I'm trying to advertise a Qt Bluetooth class in peripheral mode using the code given in this example directly from the Qt Documentation Page.
I'm using Linux(Kubuntu 18.0.1) with C++ code. My Qt version is 5.7 with bluez version 5.48-0ubuntu3.1
My code looks like this the following:
bleHololenseServer.h:
#ifndef BLEHOLOLENSESERVER_H
#define BLEHOLOLENSESERVER_H
#include <QDebug>
#include <QObject>
#include <QLowEnergyAdvertisingData>
#include <QLowEnergyAdvertisingParameters>
#include <QLowEnergyServiceData>
#include <QLowEnergyCharacteristicData>
#include <QLowEnergyDescriptorData>
#include <QLowEnergyController>
#include <QLowEnergyService>
/*
* Visualization Class for the AnkiCar and street
*/
class BLEHololenseServer : public QObject{
Q_OBJECT
public:
BLEHololenseServer();
void startAdvertisting();
private:
signals:
public slots:
};
#endif // BLEHOLOLENSESERVER_H
bleHololenseSever.cpp
#include <QDebug>
#include <QLowEnergyAdvertisingData>
#include <QLowEnergyAdvertisingParameters>
#include <QLowEnergyServiceData>
#include <QLowEnergyCharacteristicData>
#include <QLowEnergyDescriptorData>
#include <QLowEnergyController>
#include <QLowEnergyService>
#include "../headers/bleServer.h"
#include <QScopedPointer>
BLEHololenseServer::BLEHololenseServer(){
}
void BLEHololenseServer::startAdvertisting(){
QLowEnergyAdvertisingData advertisingData;
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
advertisingData.setIncludePowerLevel(true);
advertisingData.setLocalName("AnkiServer");
advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);
QLowEnergyCharacteristicData charData;
charData.setUuid(QBluetoothUuid::HeartRateMeasurement);
charData.setValue(QByteArray(2, 0));
charData.setProperties(QLowEnergyCharacteristic::Notify);
const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration, QByteArray(2, 0));
charData.addDescriptor(clientConfig);
QLowEnergyServiceData serviceData;
serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
serviceData.setUuid(QBluetoothUuid::HeartRate);
serviceData.addCharacteristic(charData);
const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
const QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
const QLowEnergyAdvertisingParameters advertisingParameters = QLowEnergyAdvertisingParameters();
leController->startAdvertising(advertisingParameters, advertisingData, advertisingData);
}
I create this class in the a QObject class called created in main.cpp and when i call the startAdertising function, i get an Segmentationfault the second i call the last line in my startAdvertising function:
leController->startAdvertising(advertisingParameters, advertisingData, advertisingData);
Backtracing the error in gdb gives me something like this:
#0 0x00007ffff71fb082 in ?? () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#1 0x00007ffff71b56b7 in QLowEnergyController::~QLowEnergyController() () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#2 0x00007ffff71b56d9 in QLowEnergyController::~QLowEnergyController() () from /home/user/Qt/5.7/gcc_64/lib/libQt5Bluetooth.so.5
#3 0x000055555557eb4e in QScopedPointerDeleter<QLowEnergyController>::cleanup (pointer=0x5555557b78b0) at ../../../../Qt/5.7/gcc_64/include/QtCore/qscopedpointer.h:60
#4 0x000055555557eaa3 in QScopedPointer<QLowEnergyController, QScopedPointerDeleter<QLowEnergyController> >::~QScopedPointer (this=0x7fffffffe108, __in_chrg=<optimized out>)
at ../../../../Qt/5.7/gcc_64/include/QtCore/qscopedpointer.h:107
#5 0x000055555557e8a2 in BLEHololenseServer::startAdvertisting (this=0x5555557b7aa0) at src/bleHololense/bleHololenseServer.cpp:35
#6 0x000055555556abab in DriveMode::DriveMode (this=0x5555557b2940, parent=0x0) at src/drivemode.cpp:87
#7 0x000055555555ccdb in main (argc=1, argv=0x7fffffffe448) at src/main.cpp:34
}
Try not to use QScopedPointer.
This code works like a charm for me:
class header:
private:
QLowEnergyController* m_controller;
source:
constructor
m_controller = QLowEnergyController::createPeripheral(this);
class::startAdvertisting() method
QLowEnergyCharacteristicData l_characteristic_data;
l_characteristic_data.setUuid(QBluetoothUuid::TemperatureMeasurement);
l_characteristic_data.setValue(QByteArray(2, 0));
l_characteristic_data.setProperties(QLowEnergyCharacteristic::Notify);
const QLowEnergyDescriptorData l_config(QBluetoothUuid::ClientCharacteristicConfiguration,
QByteArray(2, 0));
l_characteristic_data.addDescriptor(l_config);
QLowEnergyServiceData l_temp_data;
l_temp_data.setType(QLowEnergyServiceData::ServiceTypePrimary);
l_temp_data.setUuid(QBluetoothUuid::Temperature);
l_temp_data.addCharacteristic(l_characteristic_data);
QLowEnergyService* l_temp_service = m_controller->addService(l_temp_data, this);
QLowEnergyAdvertisingData l_advertising_data;
l_advertising_data.setLocalName("FakeBle");
l_advertising_data.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::Temperature);
m_controller->startAdvertising(QLowEnergyAdvertisingParameters(), l_advertising_data, l_advertising_data);
This question already has answers here:
When should Q_OBJECT be used?
(4 answers)
QT "No such slot" Error [duplicate]
(1 answer)
Closed 6 years ago.
I have written an UDP programme with Qt, and when I connect this:
connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
the complier tells me that
no such slot
the error click here
and I want to know how to fix it, thank you!
P.S.
Here are my files:
files
Here are my codes:
enter code here
udptest.cpp:
#include "udptest.h"
#include <QObject>
#include <QUdpSocket>
#include <QtNetwork>
UDPtest::UDPtest()
{
socket = new QUdpSocket();
port = 2016;
socket->bind(port,QUdpSocket::ShareAddress
| QUdpSocket::ReuseAddressHint);
connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
}
QString UDPtest::getIP()
{
QList<QHostAddress> list = QNetworkInterface::allAddresses();
foreach (QHostAddress address, list)
{
if(address.protocol() == QAbstractSocket::IPv4Protocol)
return address.toString();
}
return 0;
}
void UDPtest::sendMessage(QString message)
{
QByteArray data;
QDataStream out(&data,QIODevice::WriteOnly);
QString localHostName = QHostInfo::localHostName();
QString address = getIP();
out <<"123"<< localHostName << address << message;
socket->writeDatagram(data,data.length(),QHostAddress::Broadcast, port);
}
void UDPtest::processPendingDatagrams()
{qDebug()<<"receive";
while(socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(),datagram.size());
QDataStream in(&datagram,QIODevice::ReadOnly);
QString userName,localHostName,ipAddress,message;
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
in >>userName >>localHostName >>ipAddress >>message;
QString msg=time+userName+localHostName+ipAddress+message;
msger=msg;
qDebug()<<msg;
}
}
QString UDPtest:: messager()
{
return msger;
}
main.cpp:
#include"udptest.h"
#include<QDebug>
#include <QtCore/QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"123";
UDPtest test;
test.sendMessage("aha");
return a.exec();
}
udptest.h:
#ifndef UDPTEST_H
#define UDPTEST_H
#include <QObject>
#include <QUdpSocket>
#include <QtCore/QCoreApplication>
#include <QtNetwork>
class UDPtest:public QObject
{
public:
UDPtest();
QString messager();
void sendMessage(QString);
private slots:
void processPendingDatagrams();
private:
QString msger;
QUdpSocket *socket;
qint16 port;
QString getIP();
};
#endif // UDPTEST_H
QudptestConsole.pro:
QT += core
QT -= gui
QT += network
CONFIG += c++11
TARGET = QudptestConsole
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
udptest.cpp
HEADERS += \
udptest.h
You have forgotten Q_OBJECT macro in UDPtest class
class UDPtest: public QObject
{
Q_OBJECT
public:
UDPtest();
.....
}
I have a simple application in Qt that has a QMainWindow and a QWebEngineView that loads an html page. I've set the QApplication name and the QMainWindow title.
app.setApplicationName("FooApp");
window.setWindowTitle("FooApp Window");
When I use a screen reader, it will read the main window title as:
FooApp C:/Users/tulio/Desktop/TestApp//bin/debug/test.exe
I just need it to read the application name, how can I do this?
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
a.setApplicationName("FooApp");
MainWindow w;
w.setWindowTitle("FooApp Window");
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include <QLayout>
#include <QApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) {
this->layout()->addWidget(&web_view_);
this->layout()->setMargin(0);
this->resize(QSize(1000, 700));
QString path = QApplication::applicationDirPath() + "/index.html";
qDebug() << QString("HTML Path %1").arg(path);
web_view_.page()->setUrl(QUrl::fromLocalFile(path));
web_view_.resize(this->size());
channel_.registerObject("Test", &test_);
web_view_.page()->setWebChannel(&channel_);
}
MainWindow::~MainWindow() {
}
void MainWindow::resizeEvent(QResizeEvent *event) {
web_view_.resize(event->size());
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWebEngineView>
#include <QResizeEvent>
#include <QWebChannel>
#include <QDebug>
class Test : public QObject
{
Q_OBJECT
public:
explicit Test(QObject *parent=0) : QObject(parent) {}
virtual ~Test() {}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QWebEngineView web_view_;
QWebChannel channel_;
Test test_;
protected:
void resizeEvent(QResizeEvent *event);
};
#endif // MAINWINDOW_H
teste.pro
QT += core gui webengine webenginewidgets webchannel
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = teste
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
index.html
<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
I think you need to use accessibleDescription : QString accessibleName : QString which holds the desciption that gets accessed by assistive technologies.
Is there a way in Qt to use just one dialog in order to select either a file or a folder?
What I mean is that I'd like to have an option, let's call it select, and by using this user could, from the same dialog, pick either a folder or a file.
There isn't a built in widget, but it is easy enough to connect a QDirModel to a QTreeView and get the selection signals.
Here is an example to get you started:
test.cpp
#include <QtGui>
#include "print.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QDirModel mdl;
QTreeView view;
Print print(&mdl);
view.setModel(&mdl);
QObject::connect(
view.selectionModel(),
SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)),
&print,
SLOT(currentChanged(const QModelIndex&,const QModelIndex&)));
view.show();
return app.exec();
}
print.h
#ifndef _PRINT_H_
#define _PRINT_H_
#include <QtGui>
class Print : public QObject
{
Q_OBJECT
public:
Print(QDirModel* mdl);
~Print();
public slots:
void currentChanged(const QModelIndex& current, const QModelIndex&);
private:
QDirModel* mModel;
Q_DISABLE_COPY(Print)
};
#endif
print.cpp
#include "print.h"
Print::Print(QDirModel* mdl) : QObject(0), mModel(mdl) {}
Print::~Print() {}
void Print::currentChanged(const QModelIndex& current, const QModelIndex&)
{
qDebug() << mModel->filePath(current);
}