0

I am designing a command log using QTextEdit and I was wondering how to put a QProgressBar, if it is possible, everytime the user interacts with the user interface and only for specific commands. For example if the user upload images than the QProgressBar should be there, if the user is only setting some controls, then it is not necessary. So far I didn't find anything that describes that.

progress bar

For example I am putting below a snipped of code that should carry the QProgressBar, (e.g. the user uploads images on a QGraphicsView and show the load percentage progress):

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 QTextEdit;
    mNewText->setReadOnly(true);
    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);

}

void MainWindow::on_originalmgA_clicked()
{
    imageOriginlUploadA();
    QSize s{32, 32};
    QTextDocumentFragment fragment;
    fragment = QTextDocumentFragment::fromHtml(
                QString(R"(<img src='/home/path/toDesktop/working.png' height="%1" width="%2">)")
                .arg(s.width())
                .arg(s.height()));
    mNewText->textCursor().insertFragment(fragment);
    mNewText->append("\n");
    mNewText->setVisible(true);
}

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();
    }
}

Is it possible to insert a QProgressBar inside a QTextEdit? And if yes, can anyone please point in any useful direction or provide some example about this issue?

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • I think that it is not possible to embed a QWidget in a QTextEdit, why do not you use a QTableView? – eyllanesc Mar 22 '19 at 20:02
  • Hi eyllanesc and thanks for your comment. If it is possible I could use a `QTableView` but how do I embed a `QProgressBar` into a `QTableView`? – Emanuele Mar 22 '19 at 20:05
  • 2
    Through a delegate: https://doc.qt.io/qt-5/qabstractitemdelegate.html#details – eyllanesc Mar 22 '19 at 20:07
  • Great! Thank you eyllanesc. Just an advice now, using a `QTableView` means that I have to carry all the icons, the comment and the `QProgressBar` into a total of three columns. Say the user has a lot of commands that are registered inside the `QTableView`. The will need to scroll down to the end to see all the commands. Wouldn't that be slow? – Emanuele Mar 22 '19 at 20:28
  • I did not understand the initial part of your comment, on the other hand How many are many commands: 1k, 1M? if they are in the order of thousands there will be no problems but remember that more rows involves more resources, plus more functionalities will also lead to consume more resources so it is the decision of the developer to find a limit between both elements. For example, one way that I have worked is to create QTableview with pages, so each page can show say 100 elements, the pages you implement with a proxy that filters from and even what row to show. – eyllanesc Mar 22 '19 at 20:36
  • It will be probably hundreds of commands to record. Also all the record will be extracted into a .txt file (but this is the last step). I never thought of using a `QTableView` with pages though and it actually seems a good idea to save space and allocate resources properly. Do you think you can share a snipped of code or a small example of this solution to get me started please? – Emanuele Mar 22 '19 at 20:40
  • see https://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-example.html – eyllanesc Mar 22 '19 at 20:43
  • This actually a great solution I never thought of using! thanks eyllanesc for the tip! I will implement that. I would like to mark your answer as correct, can you re-post this so that I can mark it? – Emanuele Mar 22 '19 at 20:47

0 Answers0