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.
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?
Related
I want to be able to change page of QStackedWidget with some kind of animation (like fade in/out or others...)
after some research I find out maybe its possible with QGraphicsOpacityEffect, then I found this codes in here
Fade In Your Widget
// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
Fade Out Your Widget
// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(1);
a->setEndValue(0);
a->setEasingCurve(QEasingCurve::OutBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
connect(a,SIGNAL(finished()),this,SLOT(hideThisWidget()));
// now implement a slot called hideThisWidget() to do
// things like hide any background dimmer, etc.
but looks like these codes have some problem when used in QWidget inside of QStackedWidget i mean widget successfully fade in and out, but after animation finish if I minimize the windows the widget will disappear completely! (Im still able to see widget in bottom right corner of my window, looks like its pos changed?!)
btw my program is frameless.
thanks for help.
here is a example from my problem
test.cpp
Test::Test(QWidget *parent)
: CustomMainWindow(parent)
{
ui.setupUi(this);
setShadow(ui.bg_app);
connect(ui.close_app_btn, &QPushButton::clicked, this, &QWidget::close);
connect(ui.minimize_app_btn, &QPushButton::clicked, this, &QWidget::showMinimized);
QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
ui.checking->setGraphicsEffect(eff); // checking is my widget inside of QStackedWidget.
QPropertyAnimation* a = new QPropertyAnimation(eff, "opacity");
a->setDuration(350);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
}
CustomMainWindow.cpp
CustomMainWindow::CustomMainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowFlags(windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
setAttribute(Qt::WA_TranslucentBackground);
}
void CustomMainWindow::setShadow(QWidget* window)
{
QGraphicsDropShadowEffect* windowShadow = new QGraphicsDropShadowEffect;
windowShadow->setBlurRadius(9.0);
windowShadow->setColor(palette().color(QPalette::Highlight));
windowShadow->setOffset(0.0);
window->setGraphicsEffect(windowShadow);
}
when I run my program with this code, at first its successfully Fade In, but if I for example minimize the window the widget move from its original position to somewhere else, look at this gif
Note: MainWindow is the name of my class.
Header file:
//...
private slots:
void animationStackedWidgets();
void whenAnimationFinish();
//....
CPP file:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->button, &QPushButton::clicked, this, &MainWindow::animationStackedWidgets);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::animationStackedWidgets()
{
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
ui->stackedWidget->setGraphicsEffect(effect);
QPropertyAnimation *anim = new QPropertyAnimation(effectSw,"opacity");
anim->setDuration(350);
anim->setStartValue(0);
anim->setEndValue(1);
anim->setEasingCurve(QEasingCurve::InBack);
anim->start(QPropertyAnimation::DeleteWhenStopped);
connect(anim, SIGNAL(finished()), this, SLOT(whenAnimationFinish()));
}
void MainWindow::whenAnimationFinish()
{
ui->stackedWidget->setGraphicsEffect(0); // remove effect
}
Following my previous post I am implementing a command log using QTextEdit. The way it works is that every time the user interacts with the user interface the action is recorded inside a QTextEdit Command Log shown below. I successfully associate an image to every action (e.g. pushing a button, checking a box etc) but as you see in the print screen below the image is not resizing and everytime the user does something, instead of having an additional line, the image is put next to the other:
What is happening:
What is expected:
Below the snipped of code:
mainwindow.h
private
QTextEdit *mNewText;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 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();
QTextDocumentFragment fragment;
fragment = QTextDocumentFragment::fromHtml("<img src='/home/path/toDesktop/working.png'>");
mNewText->textCursor().insertFragment(fragment);
mNewText->setVisible(true);
}
// Here we record the activity of loading images using a QPushButton
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();
}
}
// Here for example we record the activity of a Checkbox
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 }");
QTextDocumentFragment fragment;
fragment = QTextDocumentFragment::fromHtml("<img src='/home/path/toDesktop/working.png'>");
mNewText->textCursor().insertFragment(fragment);
mNewText->setVisible(true);
}
else {
ui->checkBoxScreen_A->setText("Inactive");
ui->saveToFile_A->setEnabled(false);
ui->saveToFile_A->setStyleSheet("QPushButton{ background-color: grey }");
}
}
}
I found this useful source that helped on how to take care of the image inside QTextEdit. However I didn't found anything that clearly explain on how to resize the QTextDocumentFragment inside a QTextEdit and its related text as shown:
How to set the size of image and text inside QTextEdit using QTextDocumentFragment?
Anyone who can provide any insight would be greatly helpful.
Since you are using HTML you must use the width and height attributes of the img tag:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextEdit w;
QSize s{16, 16};
for (int i=0; i<4; ++i) {
s *= 2;
QTextDocumentFragment fragment;
fragment = QTextDocumentFragment::fromHtml(
QString(R"(<img src=':/qt-logo.png' height="%1" width="%2">)")
.arg(s.width())
.arg(s.height()));
w.textCursor().insertFragment(fragment);
}
w.resize(640, 480);
w.show();
return a.exec();
}
In my project, I am using two QPushButton and two QLineEdit. I am connecting these QPushButton with these QLineEdit in such a way, so that QPushButton allow the user to select a folder from hard drive and after selection, the corresponding QLineEdit will display the URL path of the selected folder.
I also like to allow the user to write the URL by himself own if he does not want to click QPushButton and choose folder. And also if the user wants, he can also edit the URL after selecting by QPushButton.
Here I am facing two problems.
1) One QLineEdit allows user to write but another one does not.
2) When user presses on QPushButton, writing mode on corresponding QLineEdit becomes disabled.
The following is the code. Here InputLine and OutputLine are two QLineEdit
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* Setting the size of Mainwindow */
this->setWindowTitle("Crop Multiple Object");
this->setFixedHeight(600);
this->setFixedWidth(800);
/* Setting QLabel for displaying Image */
QLabel* image= new QLabel(this);
image->setGeometry(20,130,500,430);
image->setStyleSheet("QLabel {background-color: rgb(200,200,200)}");
image->show();
/* Set input URL */
QPushButton* InputURL = new QPushButton(this);
InputURL->setText("Input URL");
InputURL->setGeometry(20,30,100,30);
connect(InputURL, SIGNAL(clicked(bool)), this, SLOT(ReceiveInputURL()));
/* Set output URL */
QPushButton* OutputURL = new QPushButton(this);
OutputURL->setText("Output URL");
OutputURL->setGeometry(20,80,100,30);
connect(OutputURL, SIGNAL(clicked(bool)), this, SLOT(ReceiveOutputURL()));
/* Set Input URL Line*/
InputLine->setGeometry(140,30,400,30);
OutputLine->setGeometry(140,80,400,30);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ReceiveInputURL()
{
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
QString dir = QFileDialog::getExistingDirectory(this, tr("Input Image File"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if(!dir.isEmpty())
{
InputLine->setText(dir + "/");
}
}
void MainWindow::ReceiveOutputURL()
{
QFileDialog dialog(this);
dialog.setViewMode(QFileDialog::Detail);
QString dir = QFileDialog::getExistingDirectory(this, tr("Output Image File"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if(!dir.isEmpty())
{
OutputLine->setText(dir+ "/");
}
}
I appreciate any help. Thanks in advance.
The problem is that you are creating your QLineEdit objects before the centralWidget of MainWindow is created. This puts the central widget on top of your QLineEdit widgets, so it blocks the mouse events from passing through. To test this, you can disable mouse events for central widget with centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);, and you will notice that your QLineEdit widgets can be accessed by mouse clicks.
However, you shouldn't place any widgets directly on the MainWindow. This is not how QMainWindow is supposed to be used. Instead you should place your widgets on the centralWidget. You should read the docs of QMainWindow to know more.
while I'm working on something in Qt5 that closely resembles a file manager, I try to implement a very basic tree view, showing only the directory names without any other information. However, (it seems that) QTreeView doesn't let me decide which columns I want to show.
Here's what I have:
// ...
QString m_path = "C:/Users/mine";
dirModel = new QFileSystemModel(this);
dirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirModel->setRootPath(m_path);
ui->treeView->setModel(dirModel);
// ...
Now my QTreeView shows more information with the name, like the size et al.; however, this is not the desired behavior.
Setting headerVisible to false removes the "headline" of my QTreeView which is OK, but how can I remove the other columns completely? I tried:
ui->treeView->hideColumn(1);
just to test if that works, but it did not change a thing.
QTreeView* treeView = new QTreeView(centralWidget());
QFileSystemModel* fsModel = new QFileSystemModel(treeView);
fsModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
fsModel->setRootPath("/home/user");
treeView->setModel(fsModel);
// first column is the name
for (int i = 1; i < fsModel->columnCount(); ++i)
treeView->hideColumn(i);
QHBoxLayout* hLayout = new QHBoxLayout(centralWidget());
hLayout->addWidget(treeView);
Another approach here (PyQt but the logic is still the same): PyQt: removing unnecessary columns
There's nothing wrong with your approach. It works as below:
mainwindow header:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QFileSystemModel * dirModel;
};
mainwindow source:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString m_path = "E:";
dirModel = new QFileSystemModel(this);
dirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirModel->setRootPath(m_path);
ui->treeView->setModel(dirModel);
ui->treeView->hideColumn(1);
}
I'm using QGraphicsView / QGraphicsScene and added a QTestEdit-Widget. Unfortunately, the TextEdit gets rendered with a gray bar below, and I'm unable to get rid of it. Does anyone know how to achieve this?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsView *view = new QGraphicsView(ui->centralWidget);
QGraphicsScene *scene = new QGraphicsScene(0, 0, 2000, 2000, view);
scene->setSceneRect(-100,-100,400,400);
view->setTransformationAnchor(QGraphicsView::NoAnchor);
view->setScene(scene);
QTextEdit *edt_test = new QTextEdit(0);
edt_test->setGeometry(10,20,80,60);
edt_test->setFrameStyle(QFrame::Plain | QFrame::Box);
scene->addWidget(edt_test);
}
using StyleSheet didn't solve the problem, but the following worked fine:
QTextEdit *edt_test = new QTextEdit(0);
QGraphicsProxyWidget *proxy = scene->addWidget(edt_test);
edt_test->setFrameStyle(QFrame::Plain | QFrame::Box);
proxy->setGeometry(QRectF(10,20,80,60));
calling setGeometry based on the ProxyWidget is the solution. Seems weird ...