Important: Please read the Qt Code of Conduct -

[SOLVED] Add a search QLineEdit to a QTableView

  • I want to implement a basic search box for my QTableView. I'm looking for an elegant solution for placing the lineEdit in reference to the QTableView.

    One way I saw in the documentation was a composite widget (i.e. a QFrame and a layout that would contain both the QTableView and the QLineEdit) but it would be a refactoring hell to have all the table-view methods reimplemented on the level of the new widget.

    Another way I saw was to to just create a widget without a parent/layout and position it "manually" above the QTableView. But I feel like that would be really an ugly way to do it.

    Are there any other solutions I can consider?

  • Hi,

    This might be a little bit a not-the-best-way but it's easy and quick:
    SearchTableView : public QFrame
    WrapperFrame( QWidget parent );
    (const) TableView
    getTableView() (const)

    TableView *view
    QLineEdit *lineEdit

    //rest of private stuff, search mechanics etc.
    That gives you definitely good layout management instead of absolute positioning.
    In that case you don't need to wrap all the TableView's functions and if need to use many them outside just take the pointer and do what you need.

  • There is another solution. You can use a trick with style sheets to put the search widget inside the table view. What you do is add padding to the widget, and use the cleared out space to put your widget. There is an "example": around on how to put a push button inside a line edit. The principle is the same here.

  • Thank you very much for your suggestions. Will look into them!

Log in to reply