PDA

View Full Version : setFilterFixedString doesn't work in my code



Koch
20th December 2014, 22:13
Hello!
First at all, my english is really really bad, sorry for this :c

Well, I maked a program, with a lineedit, this lineedit is for filtering the database

the database is a .sqlite file

The database was edited in navicat for sqlite

My SO is Windows XP SP 3 and my qt version is QT 5.0.2, but i tried this code in 5.2.1 and 5.3.2 and doesn't work ...
and the compiler is Minwg 4.7 , but i tried with 4.8 and 4.8.2

this is my dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QSqlRelationalTableModel>
#include <QSortFilterProxyModel>
#include <cn.h>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
QSqlRelationalTableModel * modPersonas;
QSortFilterProxyModel * proxyPersonas;


private slots:
void on_lineEdit_textChanged(const QString &arg1);

private:
Ui::Dialog *ui;
};

#endif // DIALOG_H


my cn.h (where i declare the database pointers)

#ifndef CN_H
#define CN_H

#include <QObject>
#include <QSqlDatabase>

class cn : public QObject
{
Q_OBJECT
public:
explicit cn(QObject *parent = 0);

static cn *_obj;
static cn *obj();

static QSqlDatabase _db;
static QSqlDatabase db();

signals:

public slots:

};

#endif // CN_H

my cn.cpp(this create the database(next i edit this database with navicat))

#include "cn.h"
#include "QDir"
#include <QDebug>


QSqlDatabase cn::_db = QSqlDatabase::addDatabase("QSQLITE");
cn *cn::_obj = new cn;

cn::cn(QObject *parent) :
QObject(parent)
{
_db.setDatabaseName(QDir::currentPath() + "/TutorilDb.sqlite");
if(_db.open()){
qDebug() << "Conexión a la base de datos exitosa =>" + _db.databaseName() + "<=";
}
else{
qDebug() << "Problemas para ejecutar la base de datos" + _db.databaseName();
}
}

cn *cn::obj()
{
if(!_obj){
_obj = new cn();
}
return _obj;
}

QSqlDatabase cn::db()
{
return _db;
}


my dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//traemos los datos a la tabla, estamos en el constructor
modPersonas = new QSqlRelationalTableModel(this, cn::db());
modPersonas->setTable("tbPersonas");
modPersonas->setRelation(modPersonas->fieldIndex("tipo"), QSqlRelation("tbTipo", "id", "tipo"));
modPersonas->select();

//filtro de busquedas
proxyPersonas = new QSortFilterProxyModel(this);
proxyPersonas->setSourceModel(modPersonas);
proxyPersonas->setFilterCaseSensitivity(Qt::CaseInsensitive);
proxyPersonas->setFilterKeyColumn(-1);

ui->cbCol->addItems(QStringList() << "Id" << "Nombre" << "Apellido");

ui->tableView->setModel(modPersonas);
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::on_lineEdit_textChanged(const QString &arg1)
{
proxyPersonas->setFilterFixedString(arg1);
}


well, here is my problem, this compile good, but, when i write in the line edit, this doesnt filter, and i don't know why


void Dialog::on_lineEdit_textChanged(const QString &arg1)
{
proxyPersonas->setFilterFixedString(arg1);
}


If you know what im doing bad, i'll be very grateful

Thanks c:

ps: sorry for spanish text in the code, i use this for know what does each part (que hace cada parte, i dont know if a translate correctly this XD)

anda_skoa
21st December 2014, 11:52
Do you see the table unfiltered?
Is the slot on_lineEdit_textChanged called?

Cheers,
_

Koch
21st December 2014, 17:51
Yes, the table doesn't filtered. This is the problem.

And, i almost sure that the function work, but, i tried put this in the constructor

proxyPersonas->setFilterFixedString("SomethingRandom");
And doesn't work.
(In this place i put strings like 1, 2, 3, 4, Joseth, and other things that are on the list and doesn't work too.)

Thx 4 answer c:

anda_skoa
22nd December 2014, 10:17
Line 23 in your code snippet.
You are using the original model on the view, not the filtered one.

Cheers,
_

Koch
22nd December 2014, 23:51
I should put the line 23 in the function? or i should put another thing?

sorry for my delay in my answer

Thx again for help

Edit: Goddam I am a complete idiot, god, ***** **** **** i have an extra cromoson or something like that, god, I Really thank you.
proxyPersonas, damn.

I really happy for your help, this forum is amazing and you are awesome :D

anda_skoa
23rd December 2014, 08:22
I should put the line 23 in the function? or i should put another thing?

I know you've got it already but I should have phrased my answer better :)



Edit: Goddam I am a complete idiot, god, ***** **** **** i have an extra cromoson or something like that, god, I Really thank you.
proxyPersonas, damn.


Hehe. Happens to all of us at times, the reason code review systems are so valuable.
Sometime one just doesn't see the code one has written but what one assumes should be there.

And as we can see from my initial response I didn't see it at first either, but instead had some ideas what the problem could be.



I really happy for your help, this forum is amazing and you are awesome :D

Thanks :)

Koch
23rd December 2014, 20:10
I have a question:
Are there a function that return the row that is selected?

For example, I click in the row 3 and this function return 3.

Thx

wysota
24th December 2014, 09:31
The view has a selection model available. In addition, the view has a clicked signal to report clicks on items.

Koch
24th December 2014, 17:33
I can't understand
At what library you mean?
Because view is very broad to search XD

If you can be more specific i'll be very grateful :D

wysota
24th December 2014, 18:50
I can't understand
At what library you mean?
Because view is very broad to search XD

If you can be more specific i'll be very grateful :D

And how many views do you have in the code you posted? I can see one, called ui->tableView.

Koch
25th December 2014, 03:06
Sorry, idiom differences XDDD now i understand, thank for patience :D