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?
Related
In an attempt to create a small search GUI i wanted that whenever a user types in a TextEdit a program searches for all plausible results and shows them in a QGroupBox inside a QScollArea.
I now want to add that when i click on the QGroupBox a new QDialog opens and shows the found result details. (That deletes itself when it closes - the small details window not the QGroupBox)
I managed to do everything except that when i click on the QGroupBox a new QDialog opens.
Here is my code:
organizer.h
class Organizer : public QMainWindow
{
Q_OBJECT
private:
QGroupBox* generateSearchResultEnclosure(const std::string& item_name,const std::string& classer_name, const unsigned row, const unsigned column);
std::vector<QGroupBox*> search_results;
QGridLayout *scroll_layout;
unsigned nRows;
public:
explicit Organizer(ItemOrganizer* item_org,QWidget *parent = 0);
~Organizer();
private slots:
void generateClickedWindowItemResult(const std::string& exact_item_name, QWidget* parent);
void on_searchField_textChanged();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void updateFormComponentAddAvailable();
void updateFormAdd2ClsrAvailable();
void updateFormInsrtClsrAvailable();
void on_addClasser_PushButton_clicked();
private:
Ui::Organizer *ui;
ComponentAdd *form_cmp_add;
AddToClasser *form_add2_clsr;
InsertClasser *form_insrt_clsr;
ItemOrganizer *item_organizer;
};
organizer.cpp
(On QTextEdit text change):
void Organizer::on_searchField_textChanged()
{
QWidget *wdg=ui->scrollArea->takeWidget();
delete wdg;
QWidget* blank_wdg=new QWidget();
scroll_layout=new QGridLayout(blank_wdg);
//widget->setLayout(layout);
ui->scrollArea->setWidget(blank_wdg);
ui->scrollArea->setWidgetResizable(true);
if ((ui->searchField->toPlainText()).isEmpty())
return;
// Searching and generating a QGroupBox for each hit
std::vector<SearchResult> res=item_organizer->search((ui->searchField->toPlainText()).toUtf8().constData());
for (SearchResult &sr:res)
{
QGroupBox *win=generateSearchResultEnclosure(sr.cmp->name,sr.classer_name,sr.row,sr.column);
scroll_layout->addWidget(win);
win->show();
}
// Corecting QScrollArea widget size to fit all QGroupBox results
unsigned row_count=0;
if (scroll_layout->count()!=0)
row_count=scroll_layout->rowCount();
if(row_count>= nRows)
{
int h = 1.0*(ui->scrollArea->width() - scroll_layout->verticalSpacing()*(nRows+1.6))/nRows;
int hCorrected = h*row_count + scroll_layout->verticalSpacing()*(row_count+2);
ui->scrollArea->widget()->setFixedHeight(hCorrected);
}
ui->scrollArea->widget()->setFixedWidth(ui->scrollArea->viewport()->width());
}
organizer.cpp (Function that generates QGroupBox):
QGroupBox* Organizer::generateSearchResultEnclosure(const std::string& item_name,const std::string& classer_name, const unsigned row, const unsigned column)
{
QGroupBox* holder=new QGroupBox();
QVBoxLayout *layout_main=new QVBoxLayout;
QGroupBox *item_grp=new QGroupBox();
QGroupBox *clsr_grp=new QGroupBox();
QGroupBox *row_grp=new QGroupBox();
QGroupBox *column_grp=new QGroupBox();
QHBoxLayout *layout_item=new QHBoxLayout;
QHBoxLayout *layout_classer=new QHBoxLayout;
QHBoxLayout *layout_row=new QHBoxLayout;
QHBoxLayout *layout_column=new QHBoxLayout;
QLabel *item_label=new QLabel(tr("Item Name: "));
QLabel *clsr_label=new QLabel(tr("Classer Name: "));
QLabel *row_label=new QLabel(tr("Row: "));
QLabel *column_label=new QLabel(tr("Column: "));
QLineEdit *item_edit=new QLineEdit();
QLineEdit *classer_edit=new QLineEdit();
QLineEdit *row_edit=new QLineEdit();
QLineEdit *column_edit=new QLineEdit();
item_edit->setReadOnly(true);
classer_edit->setReadOnly(true);
row_edit->setReadOnly(true);
column_edit->setReadOnly(true);
item_edit->setText(item_name.c_str());
classer_edit->setText(classer_name.c_str());
row_edit->setText(QString::number(row));
column_edit->setText(QString::number(column));
layout_item->addWidget(item_label);
layout_item->addWidget(item_edit);
layout_classer->addWidget(clsr_label);
layout_classer->addWidget(classer_edit);
layout_row->addWidget(row_label);
layout_row->addWidget(row_edit);
layout_column->addWidget(column_label);
layout_column->addWidget(column_edit);
item_grp->setLayout(layout_item);
clsr_grp->setLayout(layout_classer);
row_grp->setLayout(layout_row);
column_grp->setLayout(layout_column);
layout_main->addWidget(item_grp);
layout_main->addWidget(clsr_grp);
layout_main->addWidget(row_grp);
layout_main->addWidget(column_grp);
layout_main->addStretch();
holder->setLayout(layout_main);
//holder->setMaximumHeight(ui->scrollArea->height()/2 - 10);
holder->setStyleSheet("QGroupBox {border: 2px solid gray; border-radius: 1px;} "
"QGroupBox::title {background-color: transparent; subcontrol-position: top left; padding:2 13px;}");
connect(holder, SIGNAL(clicked(bool)),this,SLOT(generateClickedWindowItemResult(item_name,holder)));
return holder;
}
Now i figured that i cannot connect that SLOT to that signal (since SIGNAL and SLOT need to have the same arguments) even though something like that would work for me.
Here is the part where i generate that QDialog in case that is needed as part of a problem solving. (Though its only form filling)
void Organizer::generateClickedWindowItemResult(const std::string& exact_item_name, QWidget *parent)
{
Component* cmp=item_organizer->getComponentByFullName(exact_item_name,true);
QDialog* cmp_window=new QDialog(parent);
QVBoxLayout *layout_main=new QVBoxLayout;
QGroupBox *item_name_grp=new QGroupBox();
QGroupBox *descr_grp=new QGroupBox();
QGroupBox *url_grp=new QGroupBox();
QGroupBox *tags_grp=new QGroupBox();
QGroupBox *quantity_grp=new QGroupBox();
QGroupBox *future_quantity_grp=new QGroupBox();
QHBoxLayout *layout_item_name=new QHBoxLayout;
QHBoxLayout *layout_descr=new QHBoxLayout;
QHBoxLayout *layout_url=new QHBoxLayout;
QHBoxLayout *layout_tags=new QHBoxLayout;
QHBoxLayout *layout_quantity=new QHBoxLayout;
QHBoxLayout *layout_future_quantity=new QHBoxLayout;
QLabel *item_name_label=new QLabel(tr("Item Name: "));
QLabel *descr_label=new QLabel(tr("Item Description: "));
QLabel *url_label=new QLabel(tr("Datasheet URL: "));
QLabel *tags_label=new QLabel(tr("Item Tags: "));
QLabel *quantity_label=new QLabel(tr("Item Current Quantity: "));
QLabel *future_quantity_label=new QLabel(tr("Item current+ordered quantity: "));
QLineEdit *item_name_edit=new QLineEdit();
QTextEdit *descr_edit=new QTextEdit();
QTextEdit *url_edit=new QTextEdit();
QTextEdit *tags_edit=new QTextEdit();
QLineEdit *quantity_edit=new QLineEdit();
QLineEdit *future_quantity_edit=new QLineEdit();
item_name_edit->setReadOnly(true);
descr_edit->setReadOnly(true);
url_edit->setReadOnly(true);
tags_edit->setReadOnly(true);
quantity_edit->setReadOnly(true);
future_quantity_edit->setReadOnly(true);
item_name_edit->setText(cmp->name.c_str());
descr_edit->setText(cmp->description.c_str());
url_edit->setText(cmp->url.c_str());
// TODO WRITE TAGS
//tags_edit->setText(cmp->tags);
quantity_edit->setText(QString::number(cmp->quantity));
future_quantity_edit->setText(QString::number(cmp->future_quantity));
layout_item_name->addWidget(item_name_label);
layout_item_name->addWidget(item_name_edit);
layout_descr->addWidget(descr_label);
layout_descr->addWidget(descr_edit);
layout_url->addWidget(url_label);
layout_url->addWidget(url_edit);
layout_tags->addWidget(tags_label);
layout_tags->addWidget(tags_edit);
layout_quantity->addWidget(quantity_label);
layout_quantity->addWidget(quantity_edit);
layout_future_quantity->addWidget(future_quantity_label);
layout_future_quantity->addWidget(future_quantity_edit);
item_name_grp->setLayout(layout_item_name);
descr_grp->setLayout(layout_descr);
url_grp->setLayout(layout_url);
tags_grp->setLayout(layout_tags);
quantity_grp->setLayout(layout_quantity);
future_quantity_grp->setLayout(layout_future_quantity);
layout_main->addWidget(item_name_grp);
layout_main->addWidget(descr_grp);
layout_main->addWidget(url_grp);
layout_main->addWidget(tags_grp);
layout_main->addWidget(quantity_grp);
layout_main->addWidget(future_quantity_grp);
layout_main->addStretch();
cmp_window->setLayout(layout_main);
cmp_window->setAttribute(Qt::WA_DeleteOnClose);
cmp_window->show();
}
How can i connect a on clicked signal from my QGroupBox to generate a QDialog and pass a parameter to QDialog as well as put that QGroupBox to be its parent?
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.
I have one window which has a button named "..." which open an other window which has also a button named "..." which open a QFileDialog. Next to the button of the second window, there are 10 QLineEdit to get 10 files. With the QFileDialog, I can't take a mp3 file and put his name in the first QLineEdit. How can I do ? And if the first QLineEdit is already took, how can I put the name of a mp3 file in the second QLineEdit ?
The file "main.h" has just the include of Qt. Here is my code and I tried many solutions without success :
#include "main.h"
int main(int argc, char **argv)
{
QApplication program(argc, argv);
/* ******************** First Window ******************** */
QWidget window;
QVBoxLayout layout;
window.setWindowTitle("Programme");
window.setWindowIcon(QIcon(":/Images/Images/Bouclier.png"));
window.setFixedSize(450, 400);
QHBoxLayout layouttop;
QLabel labeltop("Nom du programme :");
QLineEdit lineedittop;
layouttop.addWidget(&labeltop);
layouttop.addWidget(&lineedittop);
QHBoxLayout layoutmiddle;
QPushButton buttonnext;
QPushButton buttonprevious;
QPushButton buttonetc("...");
QLineEdit line0;
QLineEdit line1;
QLineEdit line2;
QLineEdit line3;
QLineEdit line4;
QLineEdit line5;
QLineEdit line6;
QLineEdit line7;
QLineEdit line8;
QLineEdit line9;
QVBoxLayout layoutleft;
layoutleft.addWidget(&buttonnext);
buttonnext.setIcon(QIcon(":/Images/Images/Haut.png"));
layoutleft.addWidget(&buttonprevious);
buttonprevious.setIcon(QIcon(":/Images/Images/Bas.png"));
QVBoxLayout layoutcenter;
layoutcenter.addWidget(&line0);
layoutcenter.addWidget(&line1);
layoutcenter.addWidget(&line2);
layoutcenter.addWidget(&line3);
layoutcenter.addWidget(&line4);
layoutcenter.addWidget(&line5);
layoutcenter.addWidget(&line6);
layoutcenter.addWidget(&line7);
layoutcenter.addWidget(&line8);
layoutcenter.addWidget(&line9);
line0.setReadOnly(true);
line1.setReadOnly(true);
line2.setReadOnly(true);
line3.setReadOnly(true);
line4.setReadOnly(true);
line5.setReadOnly(true);
line6.setReadOnly(true);
line7.setReadOnly(true);
line8.setReadOnly(true);
line9.setReadOnly(true);
QVBoxLayout layoutright;
layoutright.addWidget(&buttonetc);
layoutmiddle.addLayout(&layoutleft);
layoutmiddle.addLayout(&layoutcenter);
layoutmiddle.addLayout(&layoutright);
QHBoxLayout layoutbottom;
QPushButton buttonapply("Apply");
QPushButton buttoncancel("Cancel");
layoutbottom.addWidget(&buttonapply);
layoutbottom.addWidget(&buttoncancel);
layout.addLayout(&layouttop);
layout.addLayout(&layoutmiddle);
layout.addLayout(&layoutbottom);
window.setLayout(&layout);
window.show();
/* ******************** First Window ******************** */
/* ******************** Second Window ******************** */
QWidget windowed;
QVBoxLayout layouted;
windowed.setWindowTitle("Session");
windowed.setWindowIcon(QIcon(":/Images/Images/Bouclier.png"));
windowed.setFixedSize(450, 400);
QHBoxLayout layouttoped;
QLabel labeltoped("Nom de la session :");
QLineEdit lineedittoped;
layouttoped.addWidget(&labeltoped);
layouttoped.addWidget(&lineedittoped);
QHBoxLayout layoutmiddled;
QPushButton buttonnexted;
QPushButton buttonprevioused;
QPushButton buttonetced("...");
QString file;
QLineEdit lined0;
QLineEdit lined1;
QLineEdit lined2;
QLineEdit lined3;
QLineEdit lined4;
QLineEdit lined5;
QLineEdit lined6;
QLineEdit lined7;
QLineEdit lined8;
QLineEdit lined9;
QComboBox box;
QLabel label("Durée :");
QVBoxLayout layoutlefted;
layoutlefted.addWidget(&buttonnexted);
buttonnexted.setIcon(QIcon(":/Images/Images/Haut.png"));
layoutlefted.addWidget(&buttonprevioused);
buttonprevioused.setIcon(QIcon(":/Images/Images/Bas.png"));
QVBoxLayout layoutcentered;
layoutcentered.addWidget(&lined0);
layoutcentered.addWidget(&lined1);
layoutcentered.addWidget(&lined2);
layoutcentered.addWidget(&lined3);
layoutcentered.addWidget(&lined4);
layoutcentered.addWidget(&lined5);
layoutcentered.addWidget(&lined6);
layoutcentered.addWidget(&lined7);
layoutcentered.addWidget(&lined8);
layoutcentered.addWidget(&lined9);
lined0.setReadOnly(true);
lined1.setReadOnly(true);
lined2.setReadOnly(true);
lined3.setReadOnly(true);
lined4.setReadOnly(true);
lined5.setReadOnly(true);
lined6.setReadOnly(true);
lined7.setReadOnly(true);
lined8.setReadOnly(true);
lined9.setReadOnly(true);
QVBoxLayout layoutrighted;
layoutrighted.addWidget(&buttonetced);
QVBoxLayout layoutmore;
QHBoxLayout layoutmored;
layoutmored.addWidget(&label);
layoutmored.addWidget(&box);
box.addItem("30");
box.addItem("60");
layoutmore.addLayout(&layoutmored);
layoutmiddled.addLayout(&layoutlefted);
layoutmiddled.addLayout(&layoutcentered);
layoutmiddled.addLayout(&layoutrighted);
layoutmiddled.addLayout(&layoutmore);
QHBoxLayout layoutbottomed;
QPushButton buttonapplyed("Apply");
QPushButton buttoncanceled("Cancel");
layoutbottomed.addWidget(&buttonapplyed);
layoutbottomed.addWidget(&buttoncanceled);
layouted.addLayout(&layouttoped);
layouted.addLayout(&layoutmiddled);
layouted.addLayout(&layoutbottomed);
windowed.setLayout(&layouted);
/* ******************** Second Window ******************** */
/* ******************** Actions First Window ******************** */
QObject::connect(&buttonetc, &QPushButton::clicked, &windowed, &QWidget::show);
/* ******************** Actions First Window ******************** */
/* ******************** Actions Second Window ******************** */
QObject::connect(&buttonetced, &QPushButton::clicked, [&windowed, &file] {file = QFileDialog::getOpenFileName(&windowed, QObject::tr("Sélectionner un fichier son ..."), "C:/", QObject::tr("Audio (*.mp3)"));});
lined0.setText(Qstring(file));
/* ******************** Actions Second Window ******************** */
return program.exec();
}
Maybe you can try to emit the address of filename in one window and then receive this address in the second window by slot function
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
I am working with qt 3.3. I need to make QDialog widget with null parent always visible not stays on top (WStyle_StaysOnTop) because this flag block access for main application. I need on screen keyboard functionality for my QDialog widget.
I hope I correctly understood the question. Here is a minimal example of what u want.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDialog *dia = new QDialog(this);
//Set the windowflags
dia->setWindowFlags(dia->windowFlags() | Qt::Tool);
dia->show();
QWidget *central = new QWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
QLineEdit *edit = new QLineEdit;
//Add sample QLineEdit to test the input focus for mainwindow
mainLayout->addWidget(edit);
central->setLayout(mainLayout);
setCentralWidget(central);
}
edit:
If you want to be able to minimize and maximize the dialog in question from systray you have to create the QSystrayIcon and context menu for it:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDialog *dia = new QDialog(this);
dia->setWindowFlags(dia->windowFlags() | Qt::Tool);
dia->show();
QWidget *central = new QWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
QLineEdit *edit = new QLineEdit;
mainLayout->addWidget(edit);
central->setLayout(mainLayout);
setCentralWidget(central);
//Create the icon for systray
//NOTE this icon is application wide
QSystemTrayIcon *icon = new QSystemTrayIcon(QIcon(QPixmap("/usr/share/icons/oxygen/22x22/status/user-away.png")), dia);
icon->setVisible(true);
//Create context menu to manipulate the dialog
QMenu *contextMenu = new QMenu;
QAction *minimizeDialog = contextMenu->addAction("Minimize dialog");
QAction *restoreDialog = contextMenu->addAction("Restore dialog");
connect(minimizeDialog, SIGNAL(triggered()), dia, SLOT(hide()));
connect(restoreDialog, SIGNAL(triggered()), dia, SLOT(show()));
//Add it to the icon
icon->setContextMenu(contextMenu);
}