1

Just to let you all know I am new to C++ Qt5. I am working on a QsqlTableModel QTableView project using an existing SQLITE db. I have two issues I am trying to resolve without any success.

  1. the buttons all work as expected until I add the proxymodel code. using the addButton without the proxymodel inserts row at the bottom of the table using scrollToBottom(); (view1.png) with proxymodel code I cannot find the code to insert row at the bottom of the table, (view2.png) the other buttons I am still working at.

  2. the proxymodel is really confusing me, it is working but not as I want it to. I have tried many combinations of code etc. but nothing else works, I am not experienced enough to work out other ways to get the results I require. I cannot fully understand the QT tutorial, I have read QSortFilterProxyModel, QRegularExpression, until it becomes a blur! I wish to enter a alphanumeric character into the lineEdit and only get the FIRST character of the entire string column entry, (view3.png) NOT the first character of any words in the string that have the same first character. (view4.png) this should be showing only "Steel" items in the column, not "600 Series" or the "Briton 2820 Series".

Any assistance will be gratefully accepted.

Thank you.

enter code here
#include "materials.h"
#include "ui_materials.h"


Materials::Materials(QWidget *parent) :
    QDialog(parent), ui(new Ui::Materials)
{
    ui->setupUi(this);

    ldb = QSqlDatabase::addDatabase("QSQLITE");
    ldb.setDatabaseName("/home/design/Desktop/Builder/Stock.db");

    if(!ldb.open())
        {
        qDebug()<<("Failed To Open the Database");
        QMessageBox::critical(this, "Cannot o#include "materials.h"
#include "ui_materials.h"


Materials::Materials(QWidget *parent) :
    QDialog(parent), ui(new Ui::Materials)
{
    ui->setupUi(this);

    ldb = QSqlDatabase::addDatabase("QSQLITE");
    ldb.setDatabaseName("/home/design/Desktop/Builder/Stock.db");

    if(!ldb.open())
        {
        qDebug()<<("Failed To Open the Database");
        QMessageBox::critical(this, "Cannot open database","Unable to establish a database connection.\n\n"
                       "Click Cancel to exit.",QMessageBox::Cancel);
        }

    model = new QSqlTableModel(this);
    model->setTable("materials");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0, Qt::Horizontal, tr("id"));
    model->setHeaderData(1, Qt::Horizontal, tr("System"));
    model->setHeaderData(2, Qt::Horizontal, tr("Part Number"));
    model->setHeaderData(3, Qt::Horizontal, tr("Product"));
    model->setHeaderData(4, Qt::Horizontal, tr("Finish"));
    model->setHeaderData(5, Qt::Horizontal, tr("Cost"));
    model->setHeaderData(6, Qt::Horizontal, tr("Qty."));
    model->setHeaderData(7, Qt::Horizontal, tr("Supplier"));
    model->select();

    while(model->canFetchMore())
    {
        model->fetchMore();
    }
    qDebug() <<(model->rowCount());
    ui->lineEditCount->setText(QString::number(model->rowCount()));

//    QRegularExpression re("^[A-Za-z0-9]{1}");
//    re.setPatternOptions(QRegularExpression::MultilineOption);

    QSortFilterProxyModel *systemFilter = new QSortFilterProxyModel(model);
    systemFilter->setSourceModel(model);
    systemFilter->setFilterKeyColumn(1);

    QSortFilterProxyModel *partNumberFilter = new QSortFilterProxyModel(model);
    partNumberFilter->setSourceModel(systemFilter);
    partNumberFilter->setFilterKeyColumn(2);

    QSortFilterProxyModel *productFilter = new QSortFilterProxyModel(model);
    productFilter->setSourceModel(partNumberFilter);
    productFilter->setFilterKeyColumn(3);

    QSortFilterProxyModel *supplierFilter = new QSortFilterProxyModel(model);
    supplierFilter->setSourceModel(productFilter);
    supplierFilter->setFilterKeyColumn(7);

    QTableView *view = ui->tableView;
    view->setModel(supplierFilter);
//    view->setModel(model);
    view->setSortingEnabled(true);
    view->setColumnHidden(0,true);
    view->horizontalHeader()->setFixedHeight(20);
    view->horizontalHeader()->setFont(QFont("Arial"));
    view->horizontalHeader()->setStyleSheet("QHeaderView {font-weight: bold; font-size: 10pt; color: #000;} ");
    view->sortByColumn(1,Qt::AscendingOrder);
    view->resizeColumnsToContents();

    connect(ui->lineEditSystem, SIGNAL(textEdited(QString)), systemFilter, SLOT(setFilterRegularExpression(QString)));
    connect(ui->lineEditPartNumber, SIGNAL(textEdited(QString)), partNumberFilter, SLOT(setFilterRegularExpression(QString)));
    connect(ui->lineEditProduct, SIGNAL(textEdited(QString)), productFilter, SLOT(setFilterRegularExpression(QString)));
    connect(ui->lineEditSupplier, SIGNAL(textEdited(QString)), supplierFilter, SLOT(setFilterRegularExpression(QString)));

}

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

void Materials::on_addRecordButton_clicked()
{
    model->insertRow(model->rowCount());
    //ui->tableView->scrollToBottom();
}

void Materials::on_updateRecordButton_clicked()
{
      model->submitAll();
    //while(model->canFetchMore())
    //{
    //    model->fetchMore();
    //    model->select();
    //}
}

void Materials::on_cancelChangesButton_clicked()
{
    model->revertAll();
    //ui->tableView->scrollToTop();
}

void Materials::on_deleteRecordButton_clicked()
{
    QMessageBox msgBox;
    msgBox.setIconPixmap(QPixmap(":/images/icon.png"));
    msgBox.setWindowTitle("Delete Record - Warning");
    msgBox.setText("Do not press the Delete button until you\n"
                   "have highlighted the row you wish to Delete.\n\n"
                   "Do You Really Want To Delete This Record?");
    QPushButton *deleteButton = msgBox.addButton(tr("Delete"), QMessageBox::ActionRole);
    deleteButton->setStyleSheet("QPushButton:hover{background-color:#FFCCCC;border:1px solid #000;color:#000;border-top-right-radius:4px;border-top-left-radius:4px;border-bottom-right-radius:4px;border-bottom-left-radius:4px;}");
    deleteButton->setFocusPolicy(Qt::NoFocus);
    deleteButton->setMinimumWidth(100);
    deleteButton->setMaximumHeight(23);
    QPushButton *cancelButton = msgBox.addButton(tr("Cancel"), QMessageBox::RejectRole);
    cancelButton->setStyleSheet("QPushButton:hover{background-color:#E0FFFF;border:1px solid #000;color:#000;border-top-right-radius:4px;border-top-left-radius:4px;border-bottom-right-radius:4px;border-bottom-left-radius:4px;}");
    cancelButton->setFocusPolicy(Qt::NoFocus);
    cancelButton->setMinimumWidth(100);
    cancelButton->setMaximumHeight(23);
    msgBox.exec();
    if (msgBox.clickedButton() == deleteButton)
    {
        model->removeRow(ui->tableView->currentIndex().row());
        model->submitAll();
    }else if (msgBox.clickedButton() == cancelButton)
    {
    }
}

//void Materials::on_informationButton_clicked()
//{
//    InfoDialog * dialog = new InfoDialog(this);

//    dialog->exec();
//}

void Materials::on_exitButton_clicked()
{
    this->close();
}

view1

view2

view3 [![view4][4]][4]

AAnderson
  • 23
  • 4
  • Your question is: "I wish to enter a alphanumeric character into the lineEdit and only get the FIRST character of the entire string column entry, (view3.png) NOT the first character of any words in the string that have the same first character. (view4.png) this should be showing only "Steel" items in the column, not "600 Series" or the "Briton 2820 Series"." So to understand it right, you want to type in the letter and the list should show just words beginning or containing S? I would like to help you figure it out, please provide a fully working executable example including db file. TUF – Ingo Mi May 24 '20 at 12:32

0 Answers0