Добавление элементов с использованием делегата в qml
Почему программа не может соединить сигнал addButtonInQml из General.cpp со слотом onAddButtonInQml из LeftPanel.qml?
main.cpp
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
ListFiles *listFiles = new ListFiles();
view.rootContext()->setContextProperty(QStringLiteral("listFiles"), listFiles);
General *general= new General(&view);
view.rootContext()->setContextProperty("general", general);
general->addButtonInList("employees found", "listOfFiles");
general->addButtonInList("Patterns for requests", "patternsForRequests");
general->addButtonInList("Request", "request");
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view.show();
return app.exec();
}
General.cpp
#include "General.h"
#include "iostream"
General::General(QQuickView* view) :
m_appView(view)
{
}
void General::addButtonInList(const QString &nameButton, const QString &page) {
QString buttonID = "button_"+nameButton;
std::replace(buttonID.begin(), buttonID.end(), ' ', '_');
std::vector<QString> button;
button.push_back(buttonID);
button.push_back(nameButton);
std::cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<std::endl;
std::cout<<buttonID.toStdString()<<std::endl;
std::cout<<nameButton.toStdString()<<std::endl;
std::cout<<""<<std::endl;
std::cout<<button[0].toStdString()<<std::endl;
std::cout<<button[1].toStdString()<<std::endl;
std::cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<std::endl;
emit addButtonInQml(button);
buttons.insert(std::pair<QString, QString>(buttonID, page));
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Rectangle {
id:root
visible: true
width: 1000
height: 650
property var widthLeftPanel: 250
color: "#FDF7E2"
LeftPanel {
id:leftPanel
anchors.top: parent.top
anchors.topMargin: 20
anchors.left: parent.left
width: root.widthLeftPanel
height: parent.height
}
}
LeftPanel.qml
import QtQuick 2.0
Item {
id:leftPanel
objectName: "leftPanel"
signal buttonClicked(string msg)
Connections{
target: general
onAddButtonInQml: {
console.log("this slot")
listModelLeftButton.append({
objectNameButton: button[0],
textButton:button[1]
})
}
}
ListView{
id:listViewLeftPanel
objectName: "listViewLeftPanel"
anchors.fill: parent
anchors.top:parent.top
clip:true
delegate:LeftButton{
z:-1
width: parent.width
objectName: model.objectNameButton
id: leftButtonDelegate
Text {
anchors.horizontalCenter: parent.horizontalCenter
font.family:"Technic"
text: model.textButton
font.pixelSize:20
}
MouseArea {
id:mouseAreaButtonPageList
anchors.fill: parent
onClicked: {
console.log("qqqqqq")
console.log(parent.objectName)
buttonClicked(parent.objectName)
}
}
}
model: ListModel {
id: listModelLeftButton
objectName: "listModelLeftButton"
property var objectNameButton
}
}
Rectangle {
id: rect
objectName: "rect"
z:0
height: parent.height
width:1
anchors.right: parent.right
anchors.top:parent.top
color: "black"
}
}