I some what understand that I need filterAcceptsRow() to filter out the duplicate items from my list model for display... but I’m not quite sure how and where to appropriate the signals.... and not sure if I have the function set up properly was hoping you could help me out....
------------proxyFilter class-------------
//---UserEvent-SortModelProxy | Constructor---//
}
//---Destructor---//
UserListModelProxy::~UserListModelProxy(){
}
//---FilterAcceptsRow removes dupicate enteries from userName drop-down list---//
bool UserListModelProxy
::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent
) const Q_DECL_OVERRIDE
{
QModelIndex mindex
= sourceModel
()->index
(sourceRow,
0, sourceParent
);
bool validName = (sourceModel()->data(mindex, UserListModel::nameRole)).toBool();
if(validName){
return true;
}else{
}
return false;
}
//---The QModelIndex class is used to locate data in a data model | Overrides lessThan()---//
//---Sort the ListView model based off the roleType---//
if(sortRole() == UserEventLog::nameRole){
QVariant nameOne
= sourceModel
()->data
(one, UserEventLog
::nameRole);
QVariant nameTwo
= sourceModel
()->data
(two, UserEventLog
::nameRole);
QString str_NameOne
= nameOne.
toString();
QString str_NameTwo
= nameTwo.
toString();
if (str_NameOne != str_NameTwo){
return str_NameOne < str_NameTwo;
}
else{
//---No Role set returns false---//
}
}
else{
return false;
}
}
//---UserEvent-SortModelProxy | Constructor---//
UserListModelProxy::UserListModelProxy(QObject *parent) : QSortFilterProxyModel(parent){
}
//---Destructor---//
UserListModelProxy::~UserListModelProxy(){
}
//---FilterAcceptsRow removes dupicate enteries from userName drop-down list---//
bool UserListModelProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const Q_DECL_OVERRIDE
{
QModelIndex mindex = sourceModel()->index(sourceRow, 0, sourceParent);
bool validName = (sourceModel()->data(mindex, UserListModel::nameRole)).toBool();
if(validName){
return true;
}else{
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
return false;
}
//---The QModelIndex class is used to locate data in a data model | Overrides lessThan()---//
bool UserListModelProxy::lessThan(const QModelIndex &one, const QModelIndex &two) const{
//---Sort the ListView model based off the roleType---//
if(sortRole() == UserEventLog::nameRole){
QVariant nameOne = sourceModel()->data(one, UserEventLog::nameRole);
QVariant nameTwo = sourceModel()->data(two, UserEventLog::nameRole);
QString str_NameOne = nameOne.toString();
QString str_NameTwo = nameTwo.toString();
if (str_NameOne != str_NameTwo){
return str_NameOne < str_NameTwo;
}
else{
//---No Role set returns false---//
return QSortFilterProxyModel::lessThan(one, two);
}
}
else{
return false;
}
}
To copy to clipboard, switch view to plain text mode
----------model class----------
//---userListModel Constructor---//
//---allocates dynamic memory on heap | Instance of proxyModel class---//
m_UserListModelProxy = new UserListModelProxy(this);
m_UserListModelProxy->setSourceModel(this);
m_UserListModelProxy->setDynamicSortFilter(true);
m_UserListModelProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
//m_UserListModelProxy->setSortRole(0);
}
//---userListModel Destructor---//
UserListModel::~UserListModel(){
}
void UserListModel::init(){
dbConnect();
selectEvent();
}
//---Function is called in xmui.cpp---//
void UserListModel::setContext(QQmlContext* root){
//---Sets rootContext for model to be used in userListModel QML---//
root->setContextProperty("UserListModel", this);
root->setContextProperty("UserListProxy", m_UserListModelProxy);
}
//---ProxyModel Sort setRoleName---//
void UserListModel::setSortRole (int role){
switch(role){
case 0:
//---idRole---//
if(m_UserListModelProxy->sortRole() == UserListModel::nameRole){
if(m_UserListModelProxy->sortOrder() == Qt::AscendingOrder)
m_UserListModelProxy->sort(0, Qt::DescendingOrder);
else{
m_UserListModelProxy->sort(0, Qt::AscendingOrder);
}
}
else{
m_UserListModelProxy->setSortRole(UserListModel::nameRole);
m_UserListModelProxy->sort(0);
}
break;
}
}
//---Connect to database---//
bool UserListModel::dbConnect(){
//---check if database is connected---//
if(!m_selectDataBase.isValid()){
qDebug() << "error in connecting to DB";
m_selectDataBase
= QSqlDatabase::addDatabase("QSQLITE",
"conn4");
m_selectDataBase.setDatabaseName(Paths::root() + "/userLog.db");
qDebug() << "database connect path: "+Paths::root()+"/userLog.db";
m_selectDataBase.open();
}
else{
qDebug() <<"connected to DB" ;
m_selectDataBase.open();
}
return m_selectDataBase.isValid();
}
int UserListModel
::rowCount(const QModelIndex &parent
) const{ Q_UNUSED(parent);
return m_userList.count();
}
QHash<int, QByteArray> UserListModel::roleNames() const{
QHash<int, QByteArray> roleNames;
roleNames.insert(nameRole, "userName");
return roleNames;
}
if (index.row() < 0 || index.row() >= m_userList.count()){
}
if(role == nameRole){
UserList userNames = m_userList.at(index.row());
text = userNames.userName;
}
return text;
}
void UserListModel::addEvent(const UserList &userName){
m_userList.insert(0, userName);
endInsertRows();
}
//---Selects data from DB for display: defualt diplays all Users---//
bool UserListModel::selectEvent(){
dbConnect();
emit showBusy(true);
QSqlQuery selectQuery
("SELECT userName FROM userlogevents", m_selectDataBase
);
if(selectQuery.exec()){
qDebug()<<"UserEventLog::selectEvent() sql statement executed fine";
}
else{
emit xmui
->alertMsg
(QMessageBox::Warning,
"Database Error Message 1",
"Error: sql select script..."+selectQuery.
lastError().
text());
qDebug()<<"UserEventLog::selectEvent() Error with sql statement execution";
return selectQuery.exec();
}
UserList userNameList;
beginResetModel();
m_userList.clear();
while (selectQuery.next()){
userNameList.userName = selectQuery.value(0).toString();
addEvent(userNameList);
}
endResetModel();
emit showBusy(false);
m_selectDataBase.close();
return selectQuery.exec();
}
//---userListModel Constructor---//
UserListModel::UserListModel(QObject *parent) : QAbstractListModel(parent){
//---allocates dynamic memory on heap | Instance of proxyModel class---//
m_UserListModelProxy = new UserListModelProxy(this);
m_UserListModelProxy->setSourceModel(this);
m_UserListModelProxy->setDynamicSortFilter(true);
m_UserListModelProxy->setSortCaseSensitivity(Qt::CaseInsensitive);
//m_UserListModelProxy->setSortRole(0);
}
//---userListModel Destructor---//
UserListModel::~UserListModel(){
}
void UserListModel::init(){
dbConnect();
selectEvent();
}
//---Function is called in xmui.cpp---//
void UserListModel::setContext(QQmlContext* root){
//---Sets rootContext for model to be used in userListModel QML---//
root->setContextProperty("UserListModel", this);
root->setContextProperty("UserListProxy", m_UserListModelProxy);
}
//---ProxyModel Sort setRoleName---//
void UserListModel::setSortRole (int role){
switch(role){
case 0:
//---idRole---//
if(m_UserListModelProxy->sortRole() == UserListModel::nameRole){
if(m_UserListModelProxy->sortOrder() == Qt::AscendingOrder)
m_UserListModelProxy->sort(0, Qt::DescendingOrder);
else{
m_UserListModelProxy->sort(0, Qt::AscendingOrder);
}
}
else{
m_UserListModelProxy->setSortRole(UserListModel::nameRole);
m_UserListModelProxy->sort(0);
}
break;
}
}
//---Connect to database---//
bool UserListModel::dbConnect(){
//---check if database is connected---//
if(!m_selectDataBase.isValid()){
qDebug() << "error in connecting to DB";
m_selectDataBase = QSqlDatabase::addDatabase("QSQLITE", "conn4");
m_selectDataBase.setDatabaseName(Paths::root() + "/userLog.db");
qDebug() << "database connect path: "+Paths::root()+"/userLog.db";
m_selectDataBase.open();
}
else{
qDebug() <<"connected to DB" ;
m_selectDataBase.open();
}
return m_selectDataBase.isValid();
}
int UserListModel::rowCount(const QModelIndex &parent) const{
Q_UNUSED(parent);
return m_userList.count();
}
QHash<int, QByteArray> UserListModel::roleNames() const{
QHash<int, QByteArray> roleNames;
roleNames.insert(nameRole, "userName");
return roleNames;
}
QVariant UserListModel::data(const QModelIndex &index, int role) const{
if (index.row() < 0 || index.row() >= m_userList.count()){
return QVariant();
}
QVariant text;
if(role == nameRole){
UserList userNames = m_userList.at(index.row());
text = userNames.userName;
}
return text;
}
void UserListModel::addEvent(const UserList &userName){
beginInsertRows(QModelIndex(), 0, 0);
m_userList.insert(0, userName);
endInsertRows();
}
//---Selects data from DB for display: defualt diplays all Users---//
bool UserListModel::selectEvent(){
dbConnect();
emit showBusy(true);
QSqlQuery selectQuery("SELECT userName FROM userlogevents", m_selectDataBase);
if(selectQuery.exec()){
qDebug()<<"UserEventLog::selectEvent() sql statement executed fine";
}
else{
emit xmui->alertMsg(QMessageBox::Warning, "Database Error Message 1", "Error: sql select script..."+selectQuery.lastError().text());
qDebug()<<"UserEventLog::selectEvent() Error with sql statement execution";
return selectQuery.exec();
}
UserList userNameList;
beginResetModel();
m_userList.clear();
while (selectQuery.next()){
userNameList.userName = selectQuery.value(0).toString();
addEvent(userNameList);
}
endResetModel();
emit showBusy(false);
m_selectDataBase.close();
return selectQuery.exec();
}
To copy to clipboard, switch view to plain text mode
--------QML code--------
//---Calls dbConnect(), archiveEvent() & selectEvent() functions after QML loads---//
Component.onCompleted: {
//---calls constructors sets proxy source---//
UserEventLog.init();
//---loads userName dropDwon---//
UserListModel.init();
listView.currentIndex = 0
listView.positionViewAtBeginning();
listView.forceActiveFocus();
}
//-------------------------------------------------------------//
//---List UserNames Currently in the database---//
ComboBox {
id: userNameDropDown
implicitHeight: 40
implicitWidth: 130
KeyNavigation.tab: beginDateTextField
activeFocusOnTab: true
model: UserListModel
textRole: "userName"
style: userListCombo
Keys.onReturnPressed: {
userNameDropDown.focus = false;
beginDateTextField.focus = true;
beginDateTextField.selectAll();
}
}
//---Calls dbConnect(), archiveEvent() & selectEvent() functions after QML loads---//
Component.onCompleted: {
//---calls constructors sets proxy source---//
UserEventLog.init();
//---loads userName dropDwon---//
UserListModel.init();
listView.currentIndex = 0
listView.positionViewAtBeginning();
listView.forceActiveFocus();
}
//-------------------------------------------------------------//
//---List UserNames Currently in the database---//
ComboBox {
id: userNameDropDown
implicitHeight: 40
implicitWidth: 130
KeyNavigation.tab: beginDateTextField
activeFocusOnTab: true
model: UserListModel
textRole: "userName"
style: userListCombo
Keys.onReturnPressed: {
userNameDropDown.focus = false;
beginDateTextField.focus = true;
beginDateTextField.selectAll();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks