I would like to add some type of validation to my QInputDialog. I use the input of the dialog to create a file system path. So I would like to exclude characters such as #$#%^&*() but keep - and _. I was thinking of applying a regexp pattern but I'm not sure of the workflow.
If its not possible or it makes sense to use something different I'm open to that as well.
This is what I'm currently using:
QString defaultText("whatever");
bool ok;
QString caseInput = QInputDialog::getText(this, tr("Input Text"), tr("New Text:"), QLineEdit::Normal, defaultText, &ok);
if (ok && !caseInput.isEmpty())
{
// do stuff
}
So if you want full control of it, you will want to make your own QDialog, add in a QLabel for the text, and add in a line edit, setup a QValidator, and access the return value afterwards.
Like so:
mydialog.h
#include <QDialog>
#include <QLineEdit>
class MyDialog : public QDialog
{
Q_OBJECT
public:
MyDialog(QWidget *parent = 0);
~MyDialog();
QString getNewValue();
signals:
//void rejected();
//void accepted();
public slots:
private:
QLineEdit * le;
};
mydialog.cpp
#include "mydialog.h"
#include <QDialogButtonBox>
#include <QRegExpValidator>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QLabel>
MyDialog::MyDialog(QWidget *parent)
: QDialog(parent)
{
le = 0;
this->setAttribute(Qt::WA_QuitOnClose, false);
QVBoxLayout * vbox = new QVBoxLayout;
vbox->addWidget(new QLabel(tr("Type in your text:")));
le = new QLineEdit();
// le->setText(tr("Profile"));
// le->selectAll();
le->setPlaceholderText(tr("Profile"));
vbox->addWidget(le);
QRegExpValidator * v = new QRegExpValidator(QRegExp("[\\w\\d_ \\.]{24}"));
le->setValidator(v);
QDialogButtonBox * buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
vbox->addWidget(buttonBox);
this->setLayout(vbox);
// connect(buttonBox, SIGNAL(accepted()), this, SIGNAL(accepted()));
// connect(buttonBox, SIGNAL(rejected()), this, SIGNAL(rejected()));
}
MyDialog::~MyDialog()
{
}
QString MyDialog::getNewValue()
{
return le->text();
}
Example usage:
MyDialog dialog;
if(dialog.exec() == QDialog::Accepted)
{
QString retVal = dialog.getNewValue();
qDebug() << "Dialog value:" << retVal;
}
Another way to achieve almost the same thing:
http://qt-project.org/doc/qt-4.8/qlineedit.html#inputMask-prop
http://qt-project.org/doc/qt-4.8/widgets-lineedits.html
If you want to use the stock getText QInputDialog you can set the field for InputMethodHint:
http://qt-project.org/doc/qt-4.8/qinputdialog.html#getText
http://qt-project.org/doc/qt-4.8/qt.html#InputMethodHint-enum
But the QRegExp is the most powerful in my opinion.
Here are some good examples of QRegExp in this class:
http://qt-project.org/doc/qt-4.8/richtext-syntaxhighlighter-highlighter-cpp.html
classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
rule.format = classFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(Qt::red);
rule.pattern = QRegExp("//[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::red);
quotationFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);
functionFormat.setFontItalic(true);
functionFormat.setForeground(Qt::blue);
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
rule.format = functionFormat;
highlightingRules.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
Hope that helps.
Related
This question already has answers here:
QWidget::setLayout: Attempting to set QLayout "" on Widget "", which already has a layout
(2 answers)
Closed 1 year ago.
what's up friends, I got the issue of the title, programm working with 3 QLineEdit where 1st it's to introduce a decimal number, 2nd a hex, and the 3rd a binary.
I got signals that works while I introduce a decimal number then it puts the hex value in the 2nd QLineEdit(it's converted by signal) , and to the same way for the binary value.
until here, before was working(but not now), after I put a code to introduce a binary or hex, then it becomes to decimal , and the other val...
I need your help, maybe I'm not seeing something .... it's literally the first time I used QGidLayout, etc etc.
my code is next:
code of convertidorNumerico.cpp
#include "convertidornumerico.h"
convertidorNumerico::convertidorNumerico(QObject *parent) : QObject(parent)
{
}
void convertidorNumerico::setDec(const QString &cadena)
{
bool ok;
int num = cadena.toInt(&ok);
if ( ok )
{
emit hexChanged(QString::number(num, 16));
emit binChanged(QString::number(num, 2));
}
else
{
emit hexChanged("");
emit binChanged("");
}
}
void convertidorNumerico::setHex(const QString &cadena)
{
bool ok;
int num = cadena.toInt(&ok, 16);
if ( ok )
{
emit decChanged(QString::number(num));
emit binChanged(QString::number(num, 2));
}
else
{
emit decChanged("");
emit binChanged("");
}
}
void convertidorNumerico::setBin(const QString &cadena)
{
bool ok;
int num = cadena.toInt(&ok);
if ( ok )
{
emit decChanged(QString::number(num));
emit hexChanged(QString::number(num, 16));
}
else
{
emit decChanged("");
emit hexChanged("");
}
}
code of convertidorNumerico.h
#ifndef CONVERTIDORNUMERICO_H
#define CONVERTIDORNUMERICO_H
#include <QObject>
class convertidorNumerico : public QObject
{
Q_OBJECT
public:
explicit convertidorNumerico( QObject *parent = nullptr );
signals:
void decChanged(const QString &);
void hexChanged(const QString &);
void binChanged(const QString &);
public slots:
void setDec(const QString &);
void setHex(const QString &);
void setBin(const QString &);
};
#endif // CONVERTIDORNUMERICO_H
code of convertidorNumericoDialogo.cpp
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QIntValidator>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include "convertidornumericodialogo.h"
#include "convertidornumerico.h"
/*convertidorNumericoDialogo::convertidorNumericoDialogo(QObject *parent) : QObject(parent)
{
}*/
convertidorNumericoDialogo::convertidorNumericoDialogo()
{
QVBoxLayout *mainLayout = new QVBoxLayout ( this );
QGridLayout *editLayout = new QGridLayout ( this );
QHBoxLayout *btnLayout = new QHBoxLayout ( this );
QLabel *decLabel = new QLabel("decimal");
QLabel *hexLabel = new QLabel("Hexadecimal");
QLabel *binLabel = new QLabel("Binario");
decEdit = new QLineEdit;
hexEdit = new QLineEdit;
binEdit = new QLineEdit;
editLayout->addWidget(decLabel, 0, 0);
editLayout->addWidget( decEdit, 0, 1 );
editLayout->addWidget( hexLabel, 1, 0 );
editLayout->addWidget( hexEdit, 1, 1 );
editLayout->addWidget(binLabel, 2, 0);
editLayout->addWidget(binEdit, 2, 1);
QPushButton *btnSalir = new QPushButton("SALIR");
btnLayout->addStretch();
btnLayout->addWidget(btnSalir);
mainLayout->addLayout(editLayout);
mainLayout->addStretch();
mainLayout->addLayout(btnLayout);
btnSalir->setDefault(true);
connect(btnSalir, SIGNAL(released()), this, SLOT(accept()));
// VALORES DE UN BYTE : 0 - 255
QIntValidator *decVal = new QIntValidator(0, 255, decEdit);
decEdit->setValidator(decVal);
// EXP REG PARA VALIDAR HEXADECIMALES DE HASTA 2 DIGITOS....
QRegularExpressionValidator *hexVal = new QRegularExpressionValidator(
QRegularExpression("^[0-9A-Fa-f]{1,2}"), hexEdit);
hexEdit->setValidator(hexVal);
// EXP REG PARA VALIDAR BINARIOS DE HASTA 8 BYTES....
QRegularExpressionValidator *binVal = new QRegularExpressionValidator(
QRegularExpression("[0-1]{1,8}"), binEdit);
binEdit->setValidator(binVal);
convertidorNumerico *convertidor = new convertidorNumerico;
connect(decEdit, SIGNAL(textChanged(QString)), convertidor, SLOT(setDec(QString)));
connect(convertidor, SIGNAL(hexChanged(QString)), hexEdit, SLOT(setText(QString)));
connect(convertidor, SIGNAL(binChanged(QString)), binEdit, SLOT(setText(QString)));
// tarea: => CONECTAR las seƱales de hexa y binario, para que ingresando esos datos, se devuelva la info en la app.
connect(hexEdit, SIGNAL(textChanged(QString)), convertidor, SLOT(setHex(QString)));
connect(binEdit, SIGNAL(textChanged(QString)), convertidor, SLOT(setBin(QString)));
connect(convertidor, SIGNAL(decChanged(QString)), decEdit, SLOT(setText(QString)));
}
code of convertidorNumericoDialogo.h
#ifndef CONVERTIDORNUMERICODIALOGO_H
#define CONVERTIDORNUMERICODIALOGO_H
#include <QDialog>
class QLineEdit;
class convertidorNumericoDialogo : public QDialog
{
Q_OBJECT
public:
//explicit convertidorNumericoDialogo(QObject *parent = nullptr);
explicit convertidorNumericoDialogo();
private:
QLineEdit *decEdit;
QLineEdit *hexEdit;
QLineEdit *binEdit;
};
#endif // CONVERTIDORNUMERICODIALOGO_H
any solution? I did everything and I didn't make it working...
You are setting multiple layouts, change this:
QVBoxLayout *mainLayout = new QVBoxLayout ( this );
QGridLayout *editLayout = new QGridLayout ( this );
QHBoxLayout *btnLayout = new QHBoxLayout ( this );
to
QVBoxLayout *mainLayout = new QVBoxLayout ( this );
QGridLayout *editLayout = new QGridLayout;
QHBoxLayout *btnLayout = new QHBoxLayout;
See https://doc.qt.io/qt-5/qlayout.html#QLayout
I am trying to have a little OpenGL drawing application which needs files. I need to open a file using the menu bar. What I want is, when a user triggers the action, there is a little popup window that allows the user to enter the input.
Is is possible to do so using Qt? If yes, how?
glmainwindow.cpp
#include "glmainwindow.h"
#include <QGroupBox>
#include <QMenuBar>
glMainWindow::glMainWindow(fileReader reader, QWidget *parent) : QMainWindow(parent)
{
// initialization(reader);
QGroupBox *box = new QGroupBox(this);
mainLayout = new QGridLayout();
glWidget = new mainWidget(reader.p1, reader.p2);
mainLayout->addWidget(glWidget, 0, 0); //glWindow, 0, 0); //instance, 0, 0); //glWindow, 0, 0); //game, 1, 0); //simpleTex, 0, 0); //cubeTextureWindow, 0, 0);
/* Above FOR simpleGame */
userInput = new QLineEdit;
mainLayout->addWidget(userInput, 1, 0);
box->setLayout(mainLayout);
setCentralWidget(box);
setGeometry(150, 200, 720, 740);
createActions();
createMenus();
}
void glMainWindow::createMenus()
{
glMenuBar = menuBar();
fileMenu = new QMenu("File", this);
fileMenu->addAction(openFileAction);
fileMenu->addAction(closeAction);
glMenuBar->addMenu(fileMenu);
}
void glMainWindow::createActions()
{
openFileAction = new QAction(tr("Open file"), this);
// connect(openFileAction, &QAction::triggered)
closeAction = new QAction("Exit", this);
connect(closeAction, &QAction::triggered, glWidget, &QWidget::close);
}
glMainWindow.h
#ifndef GLMAINWINDOW_H
#define GLMAINWINDOW_H
#include <QPushButton>
#include <QLabel>
#include <QMainWindow>
#include <QGridLayout>
#include <QSlider>
#include <QLineEdit>
#include <QAction>
#include "../roadsFileRead/filereader.h"
#include "mainwidget.h"
class glMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit glMainWindow(fileReader reader, QWidget *parent = nullptr);
private:
void createMenus();
void createActions();
private:
QGridLayout *mainLayout;
mainWidget *glWidget{nullptr};
QSlider* xSlider;
QSlider* ySlider;
QSlider* zSlider;
QLineEdit *userInput;
QMenuBar *glMenuBar;
QMenu *fileMenu;
QAction *closeAction{nullptr};
QAction *openFileAction{nullptr};
};
#endif // GLMAINWINDOW_H
I have tried searching online and similar stackoverflow questions, but to no avail. However, I have seen some applications do this. I found similar tutorials, but they didn't have anything that's like what I want. How can I connect this to the triggering of the action? Also, I am not using Qt Designer.
you are almost there... just add a QfileDialog as suggested in the comments..
void glMainWindow::createActions()
{
//define the object for the file name:
QString fileName = "";
openFileAction = new QAction(tr("Open file"), this);
connect(openFileAction, &QAction::triggered, []()
{
fileName = QFileDialog::getOpenFileName(this,
tr("Open the file"), "/home/user/path", tr("my Files (*.txt *.csv)"));
});
....
}
So I got my own way. I used QInputDialog NOT QFileDialog, as QFileDialog doesn't help and is confusing. I added this to glmainwindow.h:
void openFileAct()
{
QString filePath = QInputDialog::getText(0, "File path",
"FIle path", QLineEdit::Normal,
"");
openFile(filePath.toStdString());
}
where openFile is the function that opens the file. I connected the action to openFileAct.
I added a QLabel inside my widget and it is editable through UI, able to set new text also, but not able to retrieve the updated text using function text();
QLabel *m_ColorName = new QLabel("_________");
m_ColorName->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);
In UI i am able to edit to new text but by calling function
m_ColorName->text();
Its giving default txt _________
Code
H file
class CascadeColorHighlightWidgetItem : public QWidget
{
Q_OBJECT
public:
CascadeColorHighlightWidgetItem(int jobID, QColor selectedColor, QWidget *parent = 0);
int getJobID();
QString getSelectedColor();
QString getColorText();
private:
QString m_titleName;
QRectF m_textRect;
QVBoxLayout *m_mainLyt;
QLineEdit *m_pTitleEditor;
QLabel *m_ColorName;
QColor m_SelectedColor;
};
Source File
CascadeColorHighlightWidgetItem::CascadeColorHighlightWidgetItem(QColor selectedColor, QWidget *parent)
: QWidget(parent),m_titleName("______"),m_SelectedColor(selectedColor)
{
setFixedHeight(40);
setContentsMargins(0,0,0,0);
setFocusPolicy(Qt::StrongFocus);
m_pTitleEditor = new QLineEdit();
m_ColorName = new QLabel("_________");
m_ColorName->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);
QFont font( "Calibri" );
font.setPixelSize(14);
m_ColorName->setFont(font);
QPixmap pixmap;
pixmap.fill(QColor("transparent"));
QWidget *pixMap = new QWidget;
pixMap->resize(100,100);
QString styl = QString("background-color: rgb(%1, %2, %3)").arg(QString::number(m_SelectedColor.red()),
QString::number(m_SelectedColor.green()),
QString::number(m_SelectedColor.blue()));
pixMap->setStyleSheet(styl);
m_ColorToStringMap.insert(m_ColorName->text(),m_SelectedColor);
QHBoxLayout * mnLyt = new QHBoxLayout(this);
mnLyt->addWidget(pixMap);
mnLyt->addWidget(m_ColorName);
mnLyt->addSpacerItem(new QSpacerItem(30, 0, QSizePolicy::Minimum, QSizePolicy::Minimum));
int width = pixMap->width();
int height = pixMap->height();
int side = qMin(width, height);
QRegion maskedRegion(width/2- side/2, height/2- side/2, 20,
20, QRegion::Ellipse);
pixMap->setMask(maskedRegion);
}
QString CascadeColorHighlightWidgetItem::getColorText()
{
qDebug() << "!!!CascadeColorHighlightWidgetItem::getColorText" << m_ColorName->text(); // returns "_________"
return m_ColorName->text();
}
It seems that although the text is editable, the data is not updated in the text, that can be solved with a hack, we use findChild to find the QTextDocument that is created when the flags are activated and get the text of that element:
QTextDocument *td = m_ColorName->findChild<QTextDocument *>();
if(td){
QString text = td->toRawText(); // >= Qt 5.9
// td->toPlainText();
// td->toHtml();
}
Example:
#include <QApplication>
#include <QLabel>
#include <QTextDocument>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget widget;
QLabel label("_________");
label.setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);
widget.setLayout(new QVBoxLayout);
widget.layout()->addWidget(&label);
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&label](){
QTextDocument *td = label.findChild<QTextDocument*>();
if(td){
qDebug()<<td->toRawText();
//qDebug()<<td->toPlainText();
//qDebug()<<td->toHtml();
}
});
timer.start(100);
widget.show();
return a.exec();
}
Output:
[...]
"________"
"________"
"_______"
"____f___"
"____f___"
"____ff___"
"____fff___"
"____fff___"
"____ffff___"
"____fffff___"
"____fffff___"
[...]
I set up a sub windwow in the QMdiArea of my mainwindow. Then I made a QDialog in which I want the user to enter the title name for the Sub window. But I always get an error when trying to Change the windowTitle() to that variable.
Is there any way to update the windowTitle()?
moduleName.cpp
#include "stdafx.h"
#include "moduleName.h"
#include "iwb4.h"
#include <Windows.h>
#include <QtGui/QAction>
#include <qdom.h>
#include <qmdiarea.h>
#include "ui_module_name.h"
#include "ui_iwb4.h"
#include <qmdisubwindow.h>
moduleName::moduleName(QDialog *parent)
: QDialog(parent)
{
ui.setupUi(this);
show();
// connect ok button to save the module name
connect(ui.okButton, SIGNAL(pressed()), this, SLOT(okClicked()));
}
moduleName::~moduleName()
{
}
void moduleName::okClicked()
{
iwb4 iwb;
QTextDocument* tName = ui.textEdit->document();
iwb.p_name = tName->toPlainText();
moduleName::close();
iwb.name();
}
moduleName.h
#ifndef MODULENAME_H
#define MODULENAME_H
#include <QtGui/QWidget>
#include "ui_module_name.h"
class moduleName : public QDialog
{
Q_OBJECT
public:
moduleName(QDialog *parent = 0);
~moduleName();
public slots:
void okClicked();
protected:
Ui::Dialog ui;
};
#endif // MODULENAME_H
iwb4.cpp
#include "stdafx.h"
#include "iwb4.h"
#include <Windows.h>
#include "ui_iwb4.h"
#include <QtGui/QAction>
#include <qdom.h>
#include <qmdiarea.h>
#include "ui_module_name.h"
#include "moduleName.h"
#include <qmdisubwindow.h>
iwb4::iwb4(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
iwb4::showMaximized();
p_name = " ";
// new module button
connect(ui.actionNewModule, SIGNAL(triggered()), this, SLOT(makeModule()));
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
iwb4::~iwb4()
{
}
void iwb4::newModule(QString name)
{
m_file = new QFile("C:\\Users\\Hanna\\Desktop\\iwb\\Projekt\\iwb4\\iwb4\\Datenmodell\\module.xml");
m_file->open(QFile::ReadWrite | QFile::Text);
QDomDocument doc;
doc.setContent(m_file);
m_file->close();
m_dockWidget = new QDockWidget(m_parent);
m_dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea);
m_dockWidget->showMaximized();
m_dockWidget->setTitleBarWidget(new QWidget());
m_pTableWidget = new QTableWidget(m_dockWidget);
m_dockWidget->setWidget(m_pTableWidget);
addDockWidget(Qt::LeftDockWidgetArea, m_dockWidget);
m_pTableWidget->setRowCount(10);
QDomElement elem = doc.documentElement();
m_pTableWidget->setColumnCount(elem.childNodes().count());
for (int i = 0; i < elem.childNodes().count(); i++)
{
QString header = elem.childNodes().at(i).toElement().attribute("Name");
m_TableHeader += header;
}
m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader);
m_pTableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
m_pTableWidget->verticalHeader()->setVisible(false);
m_pTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
m_pTableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
m_pTableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_pTableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_pTableWidget->setShowGrid(true);
m_pTableWidget->resizeColumnsToContents();
m_pTableWidget->resizeRowsToContents();
m_pTableWidget->setMaximumWidth(400);
m_pTableWidget->setMaximumHeight(300);
connect( m_pTableWidget, SIGNAL( cellDoubleClicked (int, int) ),
this, SLOT( cellSelected( int, int ) ) );
}
void iwb4::makeModule()
{
QString name;
name = p_name;
newModule(name);
QDockWidget *dock = m_dockWidget;
m_subWindow = ui.mdiArea->addSubWindow(dock);
ui.mdiArea->DontMaximizeSubWindowOnActivation;
dock->show();
dock->activateWindow();
// make rename option in right click menu
QMenu *menu = m_subWindow->systemMenu();
rename = new QAction(tr("Rename"),menu);
menu->addAction(rename);
connect(rename, SIGNAL(triggered()), this, SLOT(newName()));
}
void iwb4::newName()
{
moduleName* p_nameDialog = new moduleName();
}
void iwb4::name()
{
QString name = p_name;
m_subWindow->setWindowTitle(name);
}
iwb4.h
#ifndef IWB4_H
#define IWB4_H
#include <QtGui/QMainWindow>
#include "ui_iwb4.h"
class iwb4 : public QMainWindow
{
Q_OBJECT
public:
iwb4(QWidget *parent = 0, Qt::WFlags flags = 0);
~iwb4();
private:
private slots:
void makeModule();
public slots:
void newName();
public:
void newModule(QString name);
void name();
QFile* m_file;
QDockWidget* m_dockWidget;
QTableWidget* m_pTableWidget;
QMdiSubWindow* m_subWindow;
QStringList m_TableHeader;
QString p_name;
QAction *rename;
protected:
Ui::iwb4Class ui;
};
#endif // IWB4_H
Thanks for your help.
windowTitle() returns a copy, so this line is useless (you are modifying the copy only):
window->windowTitle() = name;
Instead, just use setWindowTitle directly:
window->setWindowTitle(name);
windowTitle() simply returns just another copy of the title of the window, so changing it does not change the title you want to change.
If you need to change the title, then directly use setWindowTitle(name); .
window->setWindowTitle(window->windowTitle()); is useless statement, because you set the title of window to its original title again.
void iwb4::name()
{
QString name;
name = p_name;
QMdiSubWindow* window;
window = m_subWindow;
window->windowTitle() = name;
window->setWindowTitle(window->windowTitle());
}
window->windowTitle( ) is a getter.
window->setWindowTitle( ) is a setter.
The getters job is to get you the value whereas the setters job is to update the value.
So running this will work:
void iwb4::name()
{
QString name;
name = "MyNewWindowName";
m_subWindow->setWindowTitle( name );
}
You have the same problem here:
void iwb4::makeModule()
{
QString name;
name = p_name;
newModule(name);
QDockWidget *dock = m_dockWidget;
m_subWindow = ui.mdiArea->addSubWindow(dock);
ui.mdiArea->DontMaximizeSubWindowOnActivation;
ui.mdiArea->windowTitle() = name;
ui.mdiArea->setWindowTitle(ui.mdiArea->windowTitle());
dock->show();
dock->activateWindow();
// make rename option in right click menu
QMenu *menu = m_subWindow->systemMenu();
rename = new QAction(tr("Rename"),menu);
menu->addAction(rename);
connect(rename, SIGNAL(triggered()), this, SLOT(newName()));
}
Change it to:
void iwb4::makeModule()
{
QString name;
name = p_name;
newModule(name);
QDockWidget *dock = m_dockWidget;
m_subWindow = ui.mdiArea->addSubWindow(dock);
ui.mdiArea->DontMaximizeSubWindowOnActivation;
//ui.mdiArea->windowTitle() = name; // <--
ui.mdiArea->setWindowTitle(name); // <--
dock->show();
dock->activateWindow();
// make rename option in right click menu
QMenu *menu = m_subWindow->systemMenu();
rename = new QAction(tr("Rename"),menu);
menu->addAction(rename);
connect(rename, SIGNAL(triggered()), this, SLOT(newName()));
}
I would like to get a set of four values from four input labels in Qt. I would like to use QInputDialog but it contains only one inputbox as a default one. So, how can I add four labels and four line-edits and get the value from it?
You don't. The documentation is pretty clear:
The QInputDialog class provides a simple convenience dialog to get a
single value from the user.
If you want multiple values, create a QDialog derived class from scratch with 4 input fields.
For example:
QDialog dialog(this);
// Use a layout allowing to have a label next to each field
QFormLayout form(&dialog);
// Add some text above the fields
form.addRow(new QLabel("The question ?"));
// Add the lineEdits with their respective labels
QList<QLineEdit *> fields;
for(int i = 0; i < 4; ++i) {
QLineEdit *lineEdit = new QLineEdit(&dialog);
QString label = QString("Value %1").arg(i + 1);
form.addRow(label, lineEdit);
fields << lineEdit;
}
// Add some standard buttons (Cancel/Ok) at the bottom of the dialog
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal, &dialog);
form.addRow(&buttonBox);
QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
// Show the dialog as modal
if (dialog.exec() == QDialog::Accepted) {
// If the user didn't dismiss the dialog, do something with the fields
foreach(QLineEdit * lineEdit, fields) {
qDebug() << lineEdit->text();
}
}
Following alexisdm's answer, here is one way to implement custom QInputDialog.
"inputdialog.h":
#ifndef INPUTDIALOG_H
#define INPUTDIALOG_H
#include <QDialog>
class QLineEdit;
class QLabel;
class InputDialog : public QDialog
{
Q_OBJECT
public:
explicit InputDialog(QWidget *parent = nullptr);
static QStringList getStrings(QWidget *parent, bool *ok = nullptr);
private:
QList<QLineEdit*> fields;
};
#endif // INPUTDIALOG_H
"inputdialog.cpp":
#include "inputdialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QFormLayout>
InputDialog::InputDialog(QWidget *parent) : QDialog(parent)
{
QFormLayout *lytMain = new QFormLayout(this);
for (int i = 0; i < 4; ++i)
{
QLabel *tLabel = new QLabel(QString("Text_%1:").arg(i), this);
QLineEdit *tLine = new QLineEdit(this);
lytMain->addRow(tLabel, tLine);
fields << tLine;
}
QDialogButtonBox *buttonBox = new QDialogButtonBox
( QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal, this );
lytMain->addWidget(buttonBox);
bool conn = connect(buttonBox, &QDialogButtonBox::accepted,
this, &InputDialog::accept);
Q_ASSERT(conn);
conn = connect(buttonBox, &QDialogButtonBox::rejected,
this, &InputDialog::reject);
Q_ASSERT(conn);
setLayout(lytMain);
}
QStringList InputDialog::getStrings(QWidget *parent, bool *ok)
{
InputDialog *dialog = new InputDialog(parent);
QStringList list;
const int ret = dialog->exec();
if (ok)
*ok = !!ret;
if (ret) {
foreach (auto field, dialog->fields) {
list << field->text();
}
}
dialog->deleteLater();
return list;
}
Now you can use getStrings() method similar to QInputDialog::getText():
QStringList list = InputDialog::getStrings(this);
if (!list.isEmpty()) {
// use list
}
Or
bool ok;
QStringList list = InputDialog::getStrings(this, &ok);
if (ok) {
// use list
}