jeudi 21 mars 2019

Qt Command Log using QListWidget

I am trying to build a command log on a user interface. Meaning, when the user click a button, check a box, upload some images etc, basically every time the user interacts with the user interface the action is recorded inside a QListWidget Command Log shown below. Basically this is how the ui looks as soon as the user run it:

start

And this is what I am try to achieve everytime the user interacts with the ui:

command_log

Below snippets of code from the constructor:

mainwindow.h

private:
    QListWidget *mNewTextSQLLog;

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mDockWidget_A = new QDockWidget(QLatin1String("Command Log"));
    mDockWidget_A->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    mDockWidget_A->setMinimumHeight(30);
    // Adding object to the DockWidget
    mNewText = new QListWidget;
    mNewText->setStyleSheet("background-color: light grey;");
    mNewText->setMinimumHeight(50);
    mNewText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    mDockWidget_A->setWidget(mNewText);
    addDockWidget(Qt::BottomDockWidgetArea, mDockWidget_A);
    resizeDocks({mDockWidget_A}, {200}, Qt::Horizontal);
}

And then some command of the ui, for example here is when the user upload images using a QPushButton and images are also shown on a QLabel:

void MainWindow::imageOriginlUploadB()
{
    dir_Original_B = QFileDialog::getExistingDirectory(this, tr("Choose an image directory to load"),
                                                     filesListRight, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    if(dir_Original_B.length() > 0){
        QImage image;
        QDir dirBObj(dir_Original_B);
        QStringList filesListRight = dirBObj.entryList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
        ui->labelOrigImageB->setPixmap(QPixmap::fromImage(image.scaled(125,125,Qt::KeepAspectRatio,Qt::SmoothTransformation)));
        for ( int i = 0 ; i < filesListRight.size() ; i++ )
        {
            ui->listWidgetOriginalImgB->addItem(filesListRight.at(i));
        }
        ui->listWidgetOriginalImgB->update();
        ui->labelOrigImageB->show();
    }
}


void MainWindow::on_originalmgB_clicked()
{
    imageOriginlUploadB();
}

or here is resizing the QGraphicsView using a QPushButton:

void MainWindow::on_fitViewBtn_clicked()
{
    ui->graphicsViewLX->fitInView(mLeftScene->sceneRect(), Qt::KeepAspectRatio);
    ui->graphicsViewRX->fitInView(mRightScene->sceneRect(), Qt::KeepAspectRatio);
}

And this is the activation of a QCheckBox:

void MainWindow::on_checkBoxScreen_A_toggled(bool checked)
{
    if(ui->checkBoxScreen_A->isEnabled()) {
        if(checked)
        {
            ui->checkBoxScreen_A->setText("Active");
            ui->saveToFile_A->setEnabled(true);
            ui->saveToFile_A->setStyleSheet("QPushButton{ background-color: green }");
        }
        else {
            ui->checkBoxScreen_A->setText("Inactive");
            ui->saveToFile_A->setEnabled(false);
            ui->saveToFile_A->setStyleSheet("QPushButton{ background-color: grey }");
        }
    }
}

How to achieve that? Thank you very much for pointing in the right direction

Aucun commentaire:

Enregistrer un commentaire