How to make QLineEdit editing enabled while text comes from a QPushButton? - c++

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.

Related

Change page of QstackedWidget with animation

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
}

How to put a QProgressBar inside a QTextEdit

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?

How to programmatically change style sheet of buttons in Qt?

I have so many buttons on a dialog and I want to change style sheets of them under some conditions.
Button object names are like below:
btn_1
btn_2
btn_3
..
btn_20
When I clicked one of these numerical buttons and later to another simple button, I want to change first clicked numerical button style sheet. How can I access that selected numerical button?
Edit:
What I mean by picture
I am trying to set colors of left column buttons (has numerically ordered object names) with right column buttons. User will be clicked numerical buttons first and then color named buttons.
You have to use the setStyleSheet method but you have to keep the reference of the button pressed, and that can be done using the sender method that returns the object that emitted the signal.
#include <QtWidgets>
class MainWindow: public QMainWindow{
Q_OBJECT
public:
MainWindow(QWidget *parent=nullptr):
QMainWindow(parent),
current_button(nullptr)
{
QWidget *widget = new QWidget;
setCentralWidget(widget);
QHBoxLayout *hlay = new QHBoxLayout(widget);
QVBoxLayout *number_lay = new QVBoxLayout;
QVBoxLayout *color_lay = new QVBoxLayout;
hlay->addLayout(number_lay);
hlay->addLayout(color_lay);
for(int i=0; i<20; i++){
QPushButton *button = new QPushButton(QString("btn_%1").arg(i+1));
connect(button, &QPushButton::clicked, this, &MainWindow::number_clicked);
number_lay->addWidget(button);
}
color_lay->addStretch();
for(const QString & colorname: {"Red", "Green", "Blue"}){
QPushButton *button = new QPushButton(colorname);
connect(button, &QPushButton::clicked, this, &MainWindow::color_clicked);
color_lay->addWidget(button);
button->setProperty("color", colorname.toLower());
button->setStyleSheet(QString("background-color: %1").arg(colorname));
}
color_lay->addStretch();
}
private slots:
void number_clicked(){
current_button = qobject_cast<QPushButton *>(sender());
}
void color_clicked(){
if(current_button){
QString colorname = sender()->property("color").toString();
current_button->setStyleSheet(QString("background-color: %1").arg(colorname));
}
}
private:
QPushButton *current_button;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "main.moc"
When you click on the first button, get its name using the method objectName(), then when you need to change the style, just specify in the method
setStyleSheet(QString(QPushButton#) + button->objectName() + QString("{ ... }");
I can write the example-program, but I do not fully understand what you want

Link QPushButton and QLineEdit

I work on Qt and I don't understand how to link QPushButton and QLineEdit. I did this for the QPushButton :
QHBoxLayout accountlayout;
QLabel accountlabel("AccountServer");
QLineEdit accountlineedit;
QPushButton accountbuttonselect("Select");
QPushButton accountbuttonlaunch("Launch");
QPushButton accountbuttonstop("Stop");
QString accountfile;
accountlayout.addWidget(&accountlabel);
accountlayout.addWidget(&accountlineedit);
accountlayout.addWidget(&accountbuttonselect);
accountlayout.addWidget(&accountbuttonlaunch);
accountlayout.addWidget(&accountbuttonstop);
QObject::connect(&accountbuttonselect,
&QPushButton::clicked,
[&window, &accountfile] {
accountfile = QFileDialog::getOpenFileName(
window,
QObject::tr("Sélectionner un exécutable ..."),
"C:/", QObject::tr("Exécutable (*.exe)"));
});
layout.addLayout(&accountlayout);
I open QFileDialog to search for an executable file and I want to set the QLineEdit to show the directory specified. How can I do that?

Issue with QLineEdit clear signal

I'm working with the Qt KDE Necessitas project. I have a project built in Qt Creator and I am installing the apk on an emulator API-15 (also tested on API-10).
The following code is setup to clear the text of two different QLineEdit objects when a button is clicked, but this isn't the case. Randomly, only one of the two QLineEdit objects are cleared.
mainwindow.h:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
public slots:
void slotClear();
private:
QLineEdit* line1;
QLineEdit* line2;
//...
};
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout* mainLayout = new QVBoxLayout;
QFormLayout* form = new QFormLayout;
line1 = new QLineEdit;
form->addRow(tr("Line 1: "), line1);
line2 = new QLineEdit;
form->addRow(tr("Line 2:"), line2);
QPushButton* button = new QPushButton;
mainLayout->addLayout(form);
mainLayout->addWidget(button);
QWidget* centralWid = new QWidget(this);
centralWid->setLayout(mainLayout);
this->setCentralWidget(centralWid);
connect(button, SIGNAL(clicked()), this, SLOT(slotClear()));
}
void MainWindow::slotClear()
{
line1->clear();
line2->clear();
}
//...
Calling the function QLineEdit::setText("") produces the same results. Additionally, connecting the clicked() signal from the button directly to the clear() slot of the QLineEdit has no effect.
I haven't been programming in Qt for very long, so I am unsure if there is something I am doing wrong. Is anybody seeing something needs to be corrected in order to have the text cleared from BOTH QLineEdits? I am not sure if this is unique to Qt itself or Qt Necessitas. Any input would be greatly appreciated.
EDIT
I have also just noticed that entering text in one line, switching to another line and entering text there, and then switching back to the original line results in the original text being erased once the field is clicked (note, the button was never clicked). I think this is a pretty clear indication that something funky is going on.
EDIT 2
Registered as a bug with KDE