Filter form updates lineedit in different form or how to access a class from different class



  • hi all,
    back to qt forum with basic questions:

    this subject was already discussed in several posts:
    "here":http://qt-project.org/forums/viewthread/17180
    and "here":http://qt-project.org/forums/viewthread/14825 very clearly explained how Not To Public widgets allowing access to other classes not owning them.

    my question is how to do it from practical point of view?

    for example I have a form StudentList with sevaral line edits:
    lEStudentName
    lEAddress
    lECountry
    lEGroup

    above these are the Labels: Name, Address, Country, Group
    and the pushButton Student Name, acting like a label/command link button
    if I click click Student, a new filter form will open - StudentFilter

    in this form I have all personal details of the students with several filters; the result of the filters are reported / shown in a tableview.

    here my problem starts : in StudentFilter I would like to double click in the tableview the student found and report his name in the StudentList.

    my question is only referring on how to connect the result of the filter from form StudentFilter with the lEStudentName from StudentList.

    how is this done in practice? the most conclusive answer found was to connect a function from StudentFilter with a function from StudentList, to avoid any encapsulation issues.

    but I have no function in StudentList for the lEStudentName.

    thank you for your feedback.

    my code for StudentFilter:

    @...
    void studentfilter::on_tblv_students_doubleClicked(const QModelIndex &index)
    {

    QString ind = ui->tblv_students->model()->data(index).toString(); //not sure yet if it's correct, but this should keep / return the index of the student to text
    

    {
    QSqlDatabase g7 = QSqlDatabase::database("conname"); //g is a temporary database
    if (g7.open())
    {

    QSqlQuery qstudent(g7);

    qstudent.prepare("SELECT name FROM studentfilter_view WHERE name = '"+ind+"'");
    qstudent.exec();

    // here I would like to connect the result of the qstudent query with the lEStudentName from StudentList form
    }

    g7.close();
    }
    ...@


  • Lifetime Qt Champion

    Hi,

    You can simply use a signal e.g. studentFound(const YourStudendClass &student) that you emit if your query returns something and have a slot in your StudentList to get it or in the widget dans handles this list

    Hope it helps



  • Hi SGaist,

    thank you for your prompt feedback. I've been trying to figure out how to use a Signal/Slot method in my example:

    so the code for the StudentFilter has been reduce to (and it's returning the info expected):

    @
    ...
    void studentfilter::on_tblv_students_doubleClicked(const QModelIndex &index)
    {
    QString ind = ui->tblv_students->model()->data(index).toString(); //it's correct and returns the value selected of the student-entry to text

     //my problem starts here: if I would like here to use the "connect (obj1, SIGNAL(), obj2, SLOT());" method, I cant' access the obj2 in StudentList
    }
    ...
    

    @

    I tried to construct the syntax of the connect ... to:
    @
    ...
    connect (ui->tblv_students, SIGNAL(doubleClicked(QModelIndex)), studentList("and here I would like to put my lineEditName"), SLOT(setText("the value "ind" returned by studentFilter"));
    ...
    @

    the problem: I cant' access the obj2 in StudentList, due to encapsulation rules.
    the question: is there any possibility to use this method and access the leStudentName as object2 in StudentList, without braking the encapsulation rules?

    thank you.


  • Lifetime Qt Champion

    Don't try to put the line edit there, it's an implementation detail, you could be using e.g. a QLabel in the the future for that.

    Just add the adequate slots in StudentList and then update the right widget in that slot. So you won't have unneeded dependencies



  • hi,

    trying to perform as you mentioned, I obtained some error after the app started:
    @QObject::connect: No such signal studentfilter::on_tblv_students_doubleClicked(QModelIndex)@

    so, if my slot
    (@void studentfilter::on_tblv_students_doubleClicked(const QModelIndex &index)@)
    was not a signal, I tried something else;
    accordingly to this entry, @http://qt-project.org/doc/qt-5/qobject.html#connect-4@
    there is a possibility to define a signal in the header file. I moved @void studentfilter::on_tblv_students_doubleClicked(const QModelIndex &index)@ from slots section to signals and then obtained another error:
    @multiple definition of 'studentfilter::on_tblv_students_doubleClicked(const QModelIndex &index)'@ from moc_studentfilter.cpp

    here I'm blocked. no idea what to do anymore.


  • Lifetime Qt Champion

    Are you talking about the signal in case you have found students in the database ?

    Just add a signal in you header like:

    @
    signals:
    void studentFound(const QString &name);
    @

    where name is the name of the student you found and emit it when you have found a student.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.