جستجو در تالارهای گفتگو
در حال نمایش نتایج برای برچسب های 'model'.
1 نتیجه پیدا شد
-
سلام. من میخوام محتویات دیتابیسم را نمایش بدم تو لیست ویو توسط کویری که در کلاس دیتابیسم ساخته میشه اما اصلا مدل منو تو کلاس usermanage.qml در لیست ویو نمیشناسه . MyModel.h: #include "mymodel.h" MyModel::MyModel(QObject *parent) : QSqlQueryModel(parent) { } void MyModel::setQuery(const QSqlQuery query) { QSqlQueryModel::setQuery(query); generateRoleNames(); } QVariant MyModel::data(const QModelIndex &index, int role) const { QVariant value; if (role<Qt::UserRole) { value = QSqlQueryModel::data(index,role); } else { int columnIdx = role - Qt::UserRole -1; QModelIndex modelindex = this->index(index.row(),columnIdx); value = QSqlQueryModel ::data(modelindex,Qt::DisplayRole); } return value; } //QHash<int, QByteArray> MyModel::roleNames() const //{ // return m_roleNames; //} void MyModel::generateRoleNames() { m_roleNames.clear(); for (int i = 0; i < record().count(); ++i) { m_roleNames.insert(Qt::UserRole+1,record().fieldName(i).toUtf8()); } } MyModel.cpp: #ifndef MYMODEL_H #define MYMODEL_H #include <QAbstractListModel> #include <QSqlRecord> #include <QSqlField> #include "db.h" class MyModel : public QSqlQueryModel { Q_OBJECT public: explicit MyModel(QObject *parent = nullptr); //my function void setQuery(const QSqlQuery query); QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QHash<int , QByteArray>roleNames() const override{ return m_roleNames; } private: void generateRoleNames(); QHash<int , QByteArray> m_roleNames; DB db; }; #endif // MYMODEL_H main.cpp: #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickStyle> int main(int argc, char *argv[]) { DB database; database.openConnection(); qmlRegisterType<MyModel>("Model", 1, 0, "myModel"); qmlRegisterType<MainWindow>("io.myqt.MainWindow", 1, 0, "MainWindow"); ... QQmlApplicationEngine engine; engine.load(QUrl(QLatin1String("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); } usermanage.qml: import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import QtQuick.Controls 2.2 import QtQuick.Controls.Material 2.2 import QtQuick.VirtualKeyboard 2.1 import io.myqt.UserManage 1.0 import Model 1.0 CustomeBackground { id:mngusr property StackView view UserManage{id:um} Component.onCompleted: myModel.updateModel(); MyButton { id:btnclose height: 45 anchors.left: parent.left anchors.leftMargin: 575 anchors.top: parent.top anchors.topMargin: 0 txt: qsTr("X") fontsize: 20 anchors.right: parent.right anchors.bottom: swipeView.top onClicked: { if(view.depth==1) { mngusr.view.pop(); view.visible=false; } else mngusr.view.pop(); } } Pane{ id: pane width: 400 height: 400 anchors.right: parent.right anchors.rightMargin: 2 anchors.left: parent.left anchors.leftMargin: 2 anchors.bottom: parent.bottom anchors.bottomMargin: 317 anchors.top: btnclose.bottom anchors.topMargin: 0 RowLayout { x: -12 y: 0 width: 716 height: 76 MyLable { id:lbl Layout.preferredHeight: 30 Layout.preferredWidth: 100 Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter // anchors.rightMargin: 2 txt : qsTr(" Personel ID :") } MyTextField{ id:mytxt Layout.preferredHeight: 40 Layout.preferredWidth: 70 clip: true Layout.maximumHeight: 40 Layout.maximumWidth: 200 hint: qsTr("Insert the personel id for search...") } MyButton{ id:btnsearch txt:qsTr("SEARCH") onClicked: { if(mytxt.text.length===0) um.fillTableView(-1); else um.fillTableView(mytxt.text); } } } } Frame{ x: 21 y: 162 ListView{ id:mylistView implicitHeight: 250 implicitWidth: 250 model: myModel delegate: RowLayout{ width: parent.width TextField { //Layout.width: 100 id: name text: UI_PID } } } } } usermanage.cpp: void UserManage::fillTableView(int id) { model.setQuery(db.UserManage(id)); } اینم کد دیتابیسم QSqlQuery DB::UserManage(int id) { QString str; str.append("SELECT ui.[UI_ID],ui.[UI_PID],ui.[UI_Admin],ui.[UI_Active]," "case when(select count(*) FROM t1 ua where ua. [UI_ID]=ui.[UI_ID] and [AT_ID]=1 and [UA_Active]=1)>0 then 1 else 0 end," "case when(select count(*) FROM t2 ua where ua.[UI_ID]=ui.[UI_ID] and [AT_ID]=2 and [UA_Active]=1)>0 then 1 else 0 end," "case when(select count(*) FROM t3 ua where ua.[UI_ID]=ui.[UI_ID] and [AT_ID]=3 and [UA_Active]=1)>0 then 1 else 0 end " "FROM t4 ui "); /*WHERE ui.[UI_Active]=1*/ if(id>-1) { str.append(" where ui.[UI_ID]=");/*and ui.[UI_ID]=*/ str.append(QString::number(id)); } return q; } من نمیدونم کجا اشتباه کردم. ممنون میشم کمکم کنید