Displaying an image



  • Hi,
    I have a QStandardItemModel which displays certain rows from a database. through a tableView.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    //    connection();
        Addview();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::Addview()
    {
        connectionNames = db.connectionNames ();
    
        if((connectionNames.contains ("qt_sql_default_connection")) == false)
            {
                db = QSqlDatabase::addDatabase ("QSQLITE");
                db.setDatabaseName (fileQstring);
                qDebug() << "Connection Display created in connection(). ";
            }
    
        qDebug() << "Open connection names: " << db.connectionNames ();
    
    
        if(!db.open ())
            {
                qDebug() << "The database (db) is NOT open!";
            }
    
        QSqlQuery query_main  ("SELECT ID,Name,Pic, Description FROM Items ORDER BY NAME ASC ");
    
        if(query_main.isActive()==true)
            {
                qDebug() << "The query is active.";
            }
        else
            {
                qDebug() << "The query is NOT active." << query_main.lastError ();
            }
    
        QStandardItemModel *smodel = new QStandardItemModel(this);
        ui->tableView->setModel (smodel);
    
        for(int row1 = 0; query_main.next (); row1++)
            {
                if(row1 == 0)
                    {
                        const QSqlRecord qRec = query_main.record();
                        qDebug() << "The number of records: " << qRec;
                        smodel->insertColumns (0, qRec.count());
                    }
                smodel->insertRow (row1);
                smodel->setData (smodel->index (row1,0),query_main.value (0).toString ());
                smodel->setData (smodel->index (row1,1),query_main.value (1).toString ());
                Pixmap.loadFromData (query_main.value (2).toByteArray ());
                Pixmap = Pixmap.scaled (100,100,Qt::KeepAspectRatio);
                smodel->setData (smodel->index (row1,2),Pixmap,Qt::DecorationRole);
                smodel->setData (smodel->index (row1,3), query_main.value (3).toString ());
                ui->tableView->setRowHeight (row1,100);
            }
        ui->tableView->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;}");
        ui->tableView->verticalHeader ()->setVisible (false);
        ui->tableView->setAlternatingRowColors (true);
        ui->tableView->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial");
        ui->tableView->setEditTriggers (QAbstractItemView::NoEditTriggers);
        smodel->setHeaderData (0,Qt::Horizontal, QObject::tr ("ID"));
        smodel->setHeaderData (1,Qt::Horizontal, QObject::tr ("Name"));
        smodel->setHeaderData (2,Qt::Horizontal, QObject::tr ("Picture"));
        smodel->setHeaderData (3,Qt::Horizontal, QObject::tr ("Description"));
        ui->tableView->setColumnWidth (1,1);
        ui->tableView->setColumnWidth (1,200);
        ui->tableView->setColumnWidth (2,90);
        ui->tableView->setColumnWidth (3,340);
        ui->tableView->setWordWrap (true);
    }
    
    void MainWindow::connection()
    {
    
        connectionNames = db.connectionNames ();
    
        if((connectionNames.contains ("Display")) == false)
            {
                db = QSqlDatabase::addDatabase ("QSQLITE","Display");
                db.setDatabaseName (fileQstring);
                qDebug() << "Connection Display created in connection(). ";
            }
    }
    
    void MainWindow::on_actionAdd_a_Friend_triggered()
    {
        Additem *mAddItem = new Additem;
        mAddItem->exec ();
    }
    
    
    void MainWindow::on_tableView_clicked(const QModelIndex &index)
    {
    	int row = index.row ();
        int column = index.column ();
        qDebug() << "Index: " << "(" << row <<"," << column << ")";
    
        if(column == 2)
        {
    	query_main.value(row);
        }
    }
    I want an option where the user can click on the 2nd column which is a picture and enlarge it. How can I get the pixmap from smodel and display it in tableView_clicked?
    Thank you.
    


  • if(column == 2)
        {
           QPixmap imageToEnlarge = index.data(Qt::DecorationRole).value<QPixmap>();
    	
        }
    

Log in to reply
 

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