I am currenctly using Qt 5.7 and I have subclassed QPushButton with the basic idea of using Qt Style sheets on QML side. Here is header file:
#ifndef LTPUSHBUTTON_H
#define LTPUSHBUTTON_H
#include <QWidget>
#include <QPushButton>
#include <QString>
/**
* @class Modified push button class
*/
class LtPushButton : public QPushButton
{
Q_OBJECT
public:
/**
* @brief Constructor
* @param parent
*/
LtPushButton(const QString& ltText=QString(),
QWidget* const parent=Q_NULLPTR);
/**
* @brief Method for settings CSS style from QML side
* @param ltCSSStyle
*/
Q_INVOKABLE void ltSetCSSStyle(const QString& ltCSSStyle);
};
#endif // LTPUSHBUTTON_H
and its implementation:
#include "ltpushbutton.h"
LtPushButton::LtPushButton(const QString <Text,
QWidget* const parent)
: QPushButton(parent)
{
this->setText(ltText);
} // constructor
void LtPushButton::ltSetCSSStyle(const QString& ltCSSStyle)
{
this->setStyleSheet(ltCSSStyle);
} // ltSetCSSStyle
The LtPushButton
type is registered in main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include "ltpushbutton.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<LtPushButton>("com.testapp.gui",
1,
0,
"LtPushButton");
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
and when I try to set stylesheet of window in main.qml
:
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import com.testapp.gui 1.0
Window
{
width: 640
height: 480
visible: true
title: qsTr("Hello World")
color: "red"
GridLayout
{
anchors.fill: parent
rows: 2
columns: 2
LtPushButton
{
id: ltUpperLeftButton
Layout.fillWidth: true
Layout.fillHeight: true
Layout.alignment: Qt.AlignHCenter|Qt.AlignVCenter
text: qsTr("One");
} // LtPushButton
Component.onCompleted:
{
ltUpperLeftButton.ltSetCSSStyle("border-top-left-radius: 20px;
border-top-right-radius: 20px;
border-bottom-left-radius: 20px;
border-bottom-right-radius: 20px;
background-color: #0047bd;
border: 2px solid #0047bd;
color: #ffffff;
outline: none;");
} // Component.onCompleted
} // GridLayout
} // Window
the app crashes. Basicly, I am trying to implement QML Button with arbitrary number of radial corners, for example, lower right corners is radially cornered, other corners are perpendicular:
or lower left is radially cornered, other corners are perpendicular:
Same applies to upper corners.