logo
down
shadow

How to show the filtered data of QSqlTableModel in another tableView in Qt?


How to show the filtered data of QSqlTableModel in another tableView in Qt?

By : NPM shah
Date : November 21 2020, 11:01 PM
Hope this helps Since you want to display both models, I think that the cleanest way to do what you want would be using a QSortFilterProxyModel subclass (I will refer to it as model2) in tableView2, override filterAcceptsRow() to accept only rows with column5 > 10, and set its source model to model1 (your original QSqlTableModel).
This will make both models synchronized together, an edit to any one of them will be applied automatically to both models.
code :
class MyFilterModel : public QSortFilterProxyModel{
public:
    explicit MyFilterModel(QObject* parent= nullptr):QSortFilterProxyModel(parent){}
    ~MyFilterModel(){}

    void setCol5Min(int val){
        col5Min= val;
        invalidateFilter();
    }

protected:
    bool filterAcceptsRow(int source_row, const QModelIndex &/*source_parent*/) const{
        //get index using source_row, and the column you want to filter
        QModelIndex index4= sourceModel()->index(source_row, 4);
        //accept the row only when col5's value is greater than col5Min
        return (sourceModel()->data(index4).toInt() > col5Min);
    }
private:
    int col5Min;
};
QSqlTableModel *model1 = new QSqlTableModel(this, db);
model1->setTable(tableName);
model1->select();
tableView1->setModel(model1);

MyFilterModel* model2= new MyFilterModel(this);
model2->setCol5Min(10);
model2->setSourceModel(&model1);
tableView2->setModel(model2);
QSqlTableModel *model1 = new QSqlTableModel(this, db);
model1->setTable(tableName);
model1->select();
tableView1->setModel(model1);

QSqlTableModel *model2 = new QSqlTableModel(this, db);
model2->setTable(tableName);
model2->setFilter("colum5 > 10");
model2->select();
tableView2->setModel(model2);


Share : facebook icon twitter icon
tableView(_: cellForRowAtIndexPath) is not called and TableView doesn't show any data

tableView(_: cellForRowAtIndexPath) is not called and TableView doesn't show any data


By : Daniel Dempsey
Date : March 29 2020, 07:55 AM
it helps some times My bad... I did not delete the autoimplemented functions. This has solved my problem.
collectionview within tableview cell doesnt refresh when tableview array is filtered in swift

collectionview within tableview cell doesnt refresh when tableview array is filtered in swift


By : Yuriy Matso
Date : March 29 2020, 07:55 AM
This might help you I have two original arrays which the following search bar func filters an array's on Venue.name that matches the search text and reloads the data. , In cellForRowAt method of the tableView do
code :
cell.collectionView.reloadData()
return cell
How to show headers from QSqlTableModel on TableView(QtQuick 2)?

How to show headers from QSqlTableModel on TableView(QtQuick 2)?


By : user2827764
Date : March 29 2020, 07:55 AM
it fixes the issue The problem is that if the Repeater model is a number "n" the modelData are the numbers from "0" to "n-1". The solution is to be able to access headerData from QML and for this Q_INVOKABLE must be used:
code :
class TableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    using QSqlRelationalTableModel::QSqlRelationalTableModel;
    Q_INVOKABLE QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const;
};
// ...
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    return QSqlRelationalTableModel::headerData(section, orientation, role);
}
// ...
// ...
Repeater {
    model: tableView.columns > 0 ? tableView.columns : 1

    Label {
        id: labelRow
        width: tableView.columnWidthProvider(index)
        height: tableView.rowHeightProvider()
        text: tableModel.headerData(modelData, Qt.Horizontal) // <---
    }
}
// ...
How to Refresh particular cell in tableview and store that changed data to show later on same tableview

How to Refresh particular cell in tableview and store that changed data to show later on same tableview


By : Linda Campbell
Date : March 29 2020, 07:55 AM
should help you out You need to maintain the state of your UITableViewCell in your tableView's dataSource model. I'll try to elaborate that through an example.
1. Let's assume you model looks something like:
code :
class Post {
    var isLiked = false
    var likeCount = 0
}
class CustomCell: UITableViewCell {
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var likeButton: UIButton!

    var post: Post?

    func configure(with post: Post) {
        countLabel.text = "\(post)"
        likeButton.isSelected = post.isLiked
    }

    @IBAction func likeButtonTapped(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if let post = post {
            post.isLiked = sender.isSelected
            if sender.isSelected {
                post.likeCount += 1
            } else {
                post.likeCount -= 1
            }
            countLabel.text = "\(post.likeCount)"
        }
    }
}
class VC: UIViewController, UITableViewDataSource {
    var posts = [Post]()

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        cell.configure(with: posts[indexPath.row])
        return cell
    }
}
How can i completely reset a tableview to show differently structured data in the same tableview?

How can i completely reset a tableview to show differently structured data in the same tableview?


By : gkmahendran09
Date : March 29 2020, 07:55 AM
hope this fix your issue kleopatra's assumption was indeed correct. I misunderstood the usage of removeAll() and assumed it would clear the list while the method only removes the columns passed as arguments. Adding new columns in a for-loop with list.add() instead of gathering them in a list and adding them with setAll() added insult to injury.
So thanks for setting me on the right track, kleopatra.
code :
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<SplitPane dividerPositions="0.9" maxHeight="400.0" maxWidth="600.0" minHeight="400.0" minWidth="600.0" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
            <ScrollPane layoutX="60.0" layoutY="52.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
               <content>
                  <AnchorPane maxHeight="360.0" maxWidth="600.0" minHeight="360.0" minWidth="600.0" prefHeight="360.0" prefWidth="600.0">
                     <children>
                        <TableView fx:id="dataTable" layoutX="14.0" layoutY="14.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
               </content>
            </ScrollPane>
         </children></AnchorPane>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
            <Button fx:id="regenerateButton" layoutX="255.0" layoutY="5.0" mnemonicParsing="false" onAction="#regenerateData" text="Regenerate" />
         </children></AnchorPane>
  </items>
</SplitPane>
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.setResizable(false);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
package sample;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;

import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;

public class Controller {
    @FXML
    private TableView dataTable;

    @FXML
    public void regenerateData() {
        Random rand = new Random();
        int columnCount = rand.nextInt(50);
        int rowCount = rand.nextInt(300);

        ObservableList<ObservableList> tableData = FXCollections.observableArrayList();

        ArrayList<TableColumn> columns = new ArrayList<>();

        for(int i = 0; i < columnCount; i++) {
            final int j = i;

            TableColumn col = new TableColumn(UUID.randomUUID().toString());

            col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
                @Override
                public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
                    return new SimpleStringProperty(param.getValue().get(j).toString());
                }
            });
            columns.add(col);
        }

        for(int i = 0; i < rowCount; i++) {
            ObservableList<String> row = FXCollections.observableArrayList();

            for(int j = 0; j < columnCount; j++) {
                row.add(UUID.randomUUID().toString().replace("-", ""));
            }

            tableData.add(row);
        }

        dataTable.setItems(tableData);

        dataTable.getColumns().setAll(columns);
    }
}
Related Posts Related Posts :
  • Overloading rules in the Qt resource system
  • QT,QTML How to add image Combobox list
  • Why can't I put a QFrame in a layout?
  • Use QML property inside a string
  • How to install a missing Qt module?
  • How to set text alignment for all the data in the tableView without using FOR loop?
  • Select both child item and its parent in QGraphicsFramework
  • How to change the focus of QLineEdit automatically to another QLineEdit after input satisfy a criterion?
  • PyQt5 on gnome has strange file dialogs
  • QtQuick.Controls 2.0 cannot accept width/height of ApplicationWindow
  • How to sort TreeView Items using QSortFilterProxyModel based on Combobox Selection
  • Qt app version autoincrement with custom target in .pro
  • How to change maxVisibleItems in QFileDialog filter combobox
  • Can't load/save JPG image in Qt
  • QT ts files merging
  • How to make the QT text to reappear(Blink) every few milli seconds
  • QWizardPage: how to re-disable "Next" button
  • How to sort QList according to a certain order (not alphabetical)?
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com