Доступ к делегату QML
В addButtonInList передается имя кнопки nameButton и имя соответствующей ей страницы page. Затем из nameButton создается buttonID, который передается в LeftPanel.qml делегату, чтобы записать этот buttonID как objectName. nameButton также передается, чтобы записать его как text. И nameButton и page добавляются в map buttons, чтобы потом эти значения сравнивать в buttonChoosed. При нажатии buttonChoosed получает имя кнопки, созданной делегатом и сравнивает с первыми значениями map buttons. Если он нашел такое значение в buttons, он вызывает слот из main.cpp и передает туда objectName кнопки. Там программа переходит по объектам leftPanel->listViewLeftPanel->listModelLeftButton. Здесь она пытается вывести в консоль потомков listModelLeftButton и элемент-потомок listModelLeftButton: кнопку с переданным objectName.
Почему программа не находит потомков listModelLeftButton и соответственно не находит требуемую кнопку? И почему она добавляет в map buttons сначала пару ("Patterns for requests", "patternsForRequests"), а не ("employees found", "listOfFiles"), ведь в main.cpp я сначала передавал функцию ("employees found", "listOfFiles")?
main.cpp
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext *context = engine.rootContext();
ListFiles *listFiles = new ListFiles();
context->setContextProperty("listFiles", listFiles);
General *general= new General();
context->setContextProperty("general", general);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject* root = engine.rootObjects()[0];
general->addButtonInList("employees found", "listOfFiles");
general->addButtonInList("Patterns for requests", "patternsForRequests");
general->addButtonInList("Request", "request");
QObject* leftPanel = root->findChild<QObject*>("leftPanel");
listFiles->addFileListInPage("../../CallsDatabase/");
QObject::connect(leftPanel, SIGNAL(buttonClicked(QString)), general, SLOT(buttonChoosed(QString)));
QObject::connect(general, &General::setPropertyObject, [root](QString nameObject, QString nameField, QVariant valueField){
QObject* leftPanelObject = root->findChild<QObject*>("leftPanel");
QObject* listViewLeftPanelObject = leftPanelObject->findChild<QObject*>("listViewLeftPanel");
QObject* aaa = listViewLeftPanelObject->findChild<QObject*>("listModelLeftButton");
QObject* button = listViewLeftPanelObject->findChild<QObject*>(nameObject);
std::cout<<nameObject.toStdString()<<std::endl;
const auto& children = aaa->children();
std::cout<<"start children list"<<std::endl;
qDebug()<<children;
std::cout<<"end children list"<<std::endl;
if (button == NULL) {
std::cout<<"don't found button"<<std::endl;
}
});
return app.exec();
}
General.cpp
#include "iostream"
General::General(QObject *parent) :
QObject(parent)
{
}
void General::buttonChoosed(const QString &name) {
std::cout<<"Name choosed button"<<name.toStdString()<<std::endl;
for (auto it = buttons.begin(); it != buttons.end(); ++it)
{
std::cout<<it->first.toStdString()<<std::endl;
if (it->first == name)
{
std::cout<<"this button choosed"<<std::endl;
emit setPropertyObject(it->first, "z", "1");
emit setPropertyObject(it->first, "color", "#E3D6AA");
emit setPropertyObject(it->second, "z", "1");]
}
else
{
std::cout<<"this button don't choosed"<<std::endl;
emit setPropertyObject(it->first, "z", -1);
emit setPropertyObject(it->first, "color", "#FDF7E2");
emit setPropertyObject(it->second, "z", -1);
}
}
}
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<<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;
emit addButtonInQml(button);
buttons.insert(std::pair<QString, QString>(buttonID, page));
}
main.qml
import QtQuick.Window 2.12
Window {
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
}
RightPanel {
id: rightPanel
anchors.top: parent.top
anchors.topMargin: 20
anchors.left: leftPanel.right
anchors.leftMargin: 20
anchors.right: parent.right
anchors.rightMargin: 20
anchors.bottom: parent.bottom
anchors.bottomMargin: 5
width: root.widthLeftPanel
height: parent.height
}
}
LeftPanel.qml
Item {
id:leftPanel
objectName: "leftPanel"
signal buttonClicked(string msg)
Connections{
target: general
onAddButtonInQml:{
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
}
}
}
LeftButton.qml
Rectangle {
objectName: "leftButton"
color: "#FDF7E2"
property var borderwidth: 1
height: 25
border.color: "black"
border.width: borderwidth
anchors.topMargin: borderwidth*(-1)
Rectangle {
anchors.topMargin: borderwidth
height: parent.height - 2*borderwidth
width: borderwidth
anchors.right: parent.right
anchors.top:parent.top
color: parent.color
}```