Is it possible to rename a qt sub window? - c++

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

Related

How to display QLabel inside qwidget from active QMdiAreaSubwindow in statusbar?

I have a app that uses QMdiArea.
I want the text in the statusbar to update when another QMdiAreaSubwindow becomes active.
So the text in the statusbar should become the same as the Qlabel text inside the QWidget which is been displayed inside the QMdiAreaSubwindow.
But i can't find a way to do this. Right now the statusbar only shows the text from latest created QMdiAreaSubwindow. But it won't update the text in the statusbar(With qlabel from the qwidget) when another QMdiAreaSubwindow is selected.
As you can see in the screenshot, the text in the statusbar keeps saying "test2", but I want it to change to "text" from the active QMdiAreaSubwindow.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <newwindow.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void NewSubWindow(QString name);
void createStatusBar(QString name);
private slots:
void on_actionNew_triggered();
void on_mdiArea_subWindowActivated(QMdiSubWindow *arg1);
private:
Ui::MainWindow *ui;
NewWindow *nDialog;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mdisubwidget.h"
#include "newwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
nDialog = new NewWindow();
connect(nDialog,&NewWindow::transmit,this,&MainWindow::NewSubWindow);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::NewSubWindow(QString name) {
// new Widget to add to mdiareasubwindow
mdisubwidget *mdiwidget = new mdisubwidget();
mdiwidget->addName(name);
mdiwidget->setWindowTitle(name);
// Create new mdiAreaSubWindow
ui->mdiArea->addSubWindow(mdiwidget);
// Show mdiArea
mdiwidget->show();
}
void MainWindow::on_actionNew_triggered()
{
nDialog->show();
}
void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)
{
mdisubwidget *mdiwidget = new mdisubwidget(arg1->widget());
qDebug() << "name" << mdiwidget->returnName();
createStatusBar(mdiwidget->returnName());
}
void MainWindow::createStatusBar(QString name)
{
statusBar()->showMessage("chart = "+name);
}
mdisubwidget.h
#ifndef MDISUBWIDGET_H
#define MDISUBWIDGET_H
#include <QWidget>
namespace Ui {
class mdisubwidget;
}
class mdisubwidget : public QWidget
{
Q_OBJECT
public:
explicit mdisubwidget(QWidget *parent = nullptr);
void addName(QString name);
QString returnName();
~mdisubwidget();
private:
Ui::mdisubwidget *ui;
};
#endif // MDISUBWIDGET_H
mdisubwidget.cpp
#include "mdisubwidget.h"
#include "ui_mdisubwidget.h"
#include "mainwindow.h"
QString currentName;
mdisubwidget::mdisubwidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::mdisubwidget)
{
ui->setupUi(this);
}
void mdisubwidget::addName(QString name) {
ui->label_2->setText(name);
currentName = name;
}
QString mdisubwidget::returnName() {
return currentName;
}
mdisubwidget::~mdisubwidget()
{
delete ui;
}
NewWindow.h:
#ifndef NEWWINDOW_H
#define NEWWINDOW_H
#include <QWidget>
namespace Ui {
class NewWindow;
}
class NewWindow : public QWidget
{
Q_OBJECT
public:
explicit NewWindow(QWidget *parent = nullptr);
~NewWindow();
signals:
void transmit(QString name);
private slots:
void on_pushButton_clicked();
private:
Ui::NewWindow *ui;
};
#endif // NEWWINDOW_H
NewWindow.cpp:
#include "newwindow.h"
#include "ui_newwindow.h"
#include "mainwindow.h"
NewWindow::NewWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::NewWindow)
{
ui->setupUi(this);
}
NewWindow::~NewWindow()
{
delete ui;
}
void NewWindow::on_pushButton_clicked()
{
QString name = ui->lineEdit->text();
emit transmit(name);
}
ok you're using Qt Designer to connect the signal of subWindowActivated to the slot of on_mdiArea_subWindowActivated of your MainWindow, double check with qDebug in your on_mdiArea_subWindowActivated function if the name of your selected sub window appears on the console as you tried to change your current mdi sub window so follow my code snippets to find your way:
connect(ui->mdiArea, &QMdiArea::subWindowActivated, this, &DesignerWindow::activeViewChanged);
activeViewChanged():
void DesignerWindow::activeViewChanged(QMdiSubWindow *activeSubWindow)
{
// checks if there is no active sub window defined or the number of subwindows
// are zero then return
if (!activeSubWindow)
return;
if (ui->mdiArea->subWindowList().count() == 0) {
ui->itemsTree->clear();
return;
}
// defines the current Midi, View and graphical Scene when current sub window changes
currentMidi = reinterpret_cast<MidiWindow*>(activeSubWindow->widget());
currentView = reinterpret_cast<HMIView*>(currentMidi->internalView());
currentScene = reinterpret_cast<HMIScene*>(currentMidi->internalScene());
ItemsToolBar::ItemType currentType = currentScene->itemType();
itemsToolBar->selectItemType(currentType);
// updates the widgets and labels in status bar related to current midi sub window
updateScale(currentView->zoomFactor() * 100);
updateSelected();
updateItemsTree();
updateRendererType();
}
for example for updating the label in the status bar that holds the zooming factor of the current mdiSubWindow I wrote the updateScale procedure as below:
void DesignerWindow::updateScale(double _scale)
{
scale = static_cast<int>(_scale);
scaleLbl->setText(QString("%1%").arg(scale));
}
and finally I've noticed that your creating a label in status bar every time that you try to update the text in it, please avoid such a procedure and create a QLabel object and add it to your status bar as a permanent widget like below:
scaleLbl = new QLabel(this);
scaleLbl->setFrameStyle(QFrame::Sunken | QFrame::Panel);
scaleLbl->setMinimumWidth(50);
statusBar()->addPermanentWidget(scaleLbl);

Is there a Qt widget that looks like a label to display text but can also be edited if double-clicked?

I want a widget in Qt that will act like a spreadsheet cell does. It can display text, then when the user double-clicks on it, it becomes editable. Once the user is done with editing and presses Enter, the text gets saved and the control is not editable anymore. If the user hits Escape while editing, then the control returns to its previous value.
One possible solution is sub-classing QWidget, QLabel and QLineEdit. Are there any other solutions available in Qt?
The following version also implements the same functionalities of your answer but instead of subclassing the QLineEdit and the QLabel only use eventFilter() and instead of managing the visibility manually let QStackedWidget do it.
#include <QApplication>
#include <QFormLayout>
#include <QKeyEvent>
#include <QLabel>
#include <QLineEdit>
#include <QStackedWidget>
#include <QVBoxLayout>
class MyEditableLabel: public QWidget{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
MyEditableLabel(QWidget *parent=nullptr):
QWidget(parent),
mLabel(new QLabel),
mLineEdit(new QLineEdit)
{
setLayout(new QVBoxLayout);
layout()->setMargin(0);
layout()->setSpacing(0);
layout()->addWidget(&stacked);
stacked.addWidget(mLabel);
stacked.addWidget(mLineEdit);
mLabel->installEventFilter(this);
mLineEdit->installEventFilter(this);
setSizePolicy(mLineEdit->sizePolicy());
connect(mLineEdit, &QLineEdit::textChanged, this, &MyEditableLabel::setText);
}
bool eventFilter(QObject *watched, QEvent *event){
if (watched == mLineEdit) {
if(event->type() == QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if(keyEvent->key() == Qt::Key_Return ||
keyEvent->key() == Qt::Key_Escape ||
keyEvent->key() == Qt::Key_Enter)
{
mLabel->setText(mLineEdit->text());
stacked.setCurrentIndex(0);
}
}
else if (event->type() == QEvent::FocusOut) {
mLabel->setText(mLineEdit->text());
stacked.setCurrentIndex(0);
}
}
else if (watched == mLabel) {
if(event->type() == QEvent::MouseButtonDblClick){
stacked.setCurrentIndex(1);
mLineEdit->setText(mLabel->text());
mLineEdit->setFocus();
}
}
return QWidget::eventFilter(watched, event);
}
QString text() const{
return mText;
}
void setText(const QString &text){
if(text == mText)
return;
mText == text;
emit textChanged(mText);
}
signals:
void textChanged(const QString & text);
private:
QLabel *mLabel;
QLineEdit *mLineEdit;
QStackedWidget stacked;
QString mText;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QFormLayout *lay = new QFormLayout(&w);
MyEditableLabel el;
lay->addRow("MyEditableLabel: ", &el);
lay->addRow("QLineEdit: ", new QLineEdit);
w.show();
return a.exec();
}
#include "main.moc"
this solution is not as sexy but probably one of the more performant solutions available to you is to use a QInputdialog to change QLabel and override the mouseDoubleClickEvent to trigger the input dialog. I as some here have learned that there is no means to Pull edited text from a QLabel. Not without changing QLabels internal code. Here's an example using a QInputDialog as means.
//intrlbl.h
#ifndef INTRLBL_H
#define INTRLBL_H
#include <QWidget>
#include <QLabel>
#include <QMouseEvent>
class intrLbl: public QLabel
{
Q_OBJECT
public:
intrLbl(QWidget *parent);
void mouseDoubleClickEvent(QMouseEvent *event) override;
QString text;
};
#endif // INTRLBL_H
//intrlbl.cpp file
#include "intrlbl.h"
#include <QDebug>
#include <QInputDialog>
intrLbl::intrLbl(QWidget *parent)
{
this->setText("Text Changeable Via Double Click QInput Dialog");
this->setFocusPolicy(Qt::ClickFocus);
this->setWordWrap(false);
}
void intrLbl::mouseDoubleClickEvent(QMouseEvent *event)
{
QString title
= QInputDialog::getText(this,
tr("Enter your Idea Title:"),
tr("Title:"), QLineEdit::Normal,
tr("enter your title here"));
if(!title.isEmpty())
{
qDebug() << "Title set to:" << title;
this->setText(title);
}
else
{
title = "Title";
this->setText(title);
}
}
One of the solutions is to have a QLineEdit and set it to read-only and style it in a way that it will look like a label. I personally do not like this solution, because it's more of a hacking approach. I have come up with something that in my opinion is pretty cool, which includes sub-classing QWidget, QLabel and QLineEdit:
Let's first introduce a model, which will be created in the sub-classed version of our QWidget and this model will be passed to its child widgets, the sub-classed versions of QLabel and QLineEdit:
Model header - mymodel.h:
#ifndef MYMODEL_H
#define MYMODEL_H
#include <QObject>
class MyModel : public QObject {
Q_OBJECT
Q_PROPERTY(Mode mode READ getMode WRITE setMode NOTIFY modeChanged)
Q_PROPERTY(QString text READ getText WRITE setText NOTIFY textChanged)
public:
enum class Mode {
ReadOnly = 0,
Edit = 1,
};
explicit MyModel(QObject* parent = nullptr);
Mode getMode() const {
return _mode;
}
const QString& getText() const {
return _text;
}
signals:
void modeChanged(Mode mode);
void textChanged(const QString& text);
public slots:
void setMode(Mode mode);
void setText(const QString& text);
private:
Mode _mode;
QString _text;
};
#endif // MYMODEL_H
Model implementation - mymodel.cpp
#include "mymodel.h"
MyModel::MyModel(QObject *parent)
: QObject(parent)
, _mode(MyModel::Mode::ReadOnly)
, _text(QString()) {
}
void MyModel::setMode(MyModel::Mode mode) {
if (_mode != mode) {
_mode = mode;
emit modeChanged(_mode);
}
}
void MyModel::setText(const QString &text) {
if (_text != text) {
_text = text;
emit textChanged(text);
}
}
As we see the model has the text, which is common for both the QLabel and the QLineEdit, and it has a mode, which can be either read only or edit mode.
The label implementation is a sub-class of Label.
Header - mylabel.h:
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
#include <QSharedPointer>
#include "mymodel.h"
class MyLabel : public QLabel {
Q_OBJECT
public:
explicit MyLabel(QWidget *parent = 0);
void setModel(QSharedPointer<MyModel> model);
protected:
void mouseDoubleClickEvent(QMouseEvent *) override;
private:
QSharedPointer<MyModel> _model;
};
#endif // MYLABEL_H
Implementation - mylabel.cpp:
#include "mylabel.h"
#include <QMouseEvent>
MyLabel::MyLabel(QWidget *parent)
: QLabel(parent) {
}
void MyLabel::setModel(QSharedPointer<MyModel> model) {
_model = model;
}
void MyLabel::mouseDoubleClickEvent(QMouseEvent *) {
_model->setText(text());
_model->setMode(MyModel::Mode::Edit);
}
As we our class MyLabel has a setModel() method, which will take the model from its parent. We are overriding the mouseDoubleClickEvent(), though which we are setting the text of the model to whatever text there is in the label, and setting the mode to edit, because when double-clicking we want to edit the text.
Now let's take a look at the QLineEdit. Our version of QLineEdit, called MyLineEdit, is listening to keyboard events and when Enter and Esc keys are pressed it either saves the text to the model, or discards it. Then it changes the mode to read-only.
MyLineEdit.h:
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QLineEdit>
#include <QSharedPointer>
#include "mymodel.h"
class MyLineEdit : public QLineEdit {
Q_OBJECT
public:
MyLineEdit(QWidget* parent = nullptr);
void setModel(QSharedPointer<MyModel> model);
protected:
void keyPressEvent(QKeyEvent* event) override;
void focusOutEvent(QFocusEvent*);
private:
QSharedPointer<MyModel> _model;
};
#endif // MYLINEEDIT_H
And here's the implementation - MyLineEdit.cpp:
#include "mylineedit.h"
#include <QKeyEvent>
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent) {
}
void MyLineEdit::setModel(QSharedPointer<MyModel> model) {
_model = model;
}
void MyLineEdit::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Enter) {
_model->setText(text());
_model->setMode(MyModel::Mode::ReadOnly);
} else if (event->key() == Qt::Key_Escape) {
_model->setMode(MyModel::Mode::ReadOnly);
} else {
QLineEdit::keyPressEvent(event);
}
}
void MyLineEdit::focusOutEvent(QFocusEvent *) {
_model->setText(text());
_model->setMode(MyModel::Mode::ReadOnly);
}
So now we have the model, we have our version of QLabel and our version of QLineEdit. What we want now is a parent widget that will contain both of them, listen to signals from the model and change its appearance based on the signals. That class is derived from QWidget and is called MyEditableLabel:
MyEditableLabel.h:
#ifndef MYEDITABLELABEL_H
#define MYEDITABLELABEL_H
#include <QSharedPointer>
#include <QWidget>
#include "mylabel.h"
#include "mylineedit.h"
class MyEditableLabel : public QWidget {
Q_OBJECT
public:
explicit MyEditableLabel(QWidget *parent = nullptr);
QString getText() const {return _text;}
private:
MyLabel *_label;
MyLineEdit *_lineEdit;
QSharedPointer<MyModel> _model;
private slots:
void onModeChanged(MyModel::Mode mode);
void onTextChanged(const QString &text);
private:
QString _text;
};
#endif // MYEDITABLELABEL_H
MyEditableLabel.cpp:
#include "myeditablelabel.h"
#include <QHBoxLayout>
MyEditableLabel::MyEditableLabel(QWidget *parent)
: QWidget(parent) {
_model = QSharedPointer<MyModel>(new MyModel());
_model->setText("Click me!");
_label = new MyLabel(this);
_label->setModel(_model);
_lineEdit = new MyLineEdit(this);
_lineEdit->setModel(_model);
_lineEdit->setReadOnly(false);
QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
mainLayout->addWidget(_label);
mainLayout->addWidget(_lineEdit);
setLayout(mainLayout);
connect(_model.data(), &MyModel::modeChanged, this, &MyEditableLabel::onModeChanged);
onModeChanged(_model->getMode());
connect(_model.data(), &MyModel::textChanged, this, &MyEditableLabel::onTextChanged);
onTextChanged(_model->getText());
}
void MyEditableLabel::onModeChanged(MyModel::Mode mode) {
_lineEdit->setVisible(mode == MyModel::Mode::Edit);
_lineEdit->selectAll();
_label->setVisible(mode == MyModel::Mode::ReadOnly);
}
void MyEditableLabel::onTextChanged(const QString &text) {
_lineEdit->setText(text);
_label->setText(text);
_text = text;
}
Usage:
Using this is pretty straightforward. If you're using the Qt Creator designer, then you want to draw a QWidget and the right click on it and promote it to MyEditableLabel and you're done. If you're not using the Qt Creator designer then you just have to create and instance of MyEditableLabel and you're in business.
Improvements:
It probably is a better idea to not create the model in the constructor of MyEditableLabel, but outside of it and have a setModel method in MyEditableLabel.

Converting a list of items in qt listwidget to radio buttons

I have created an application where files from a directory are being displayed in the qlistwidget.
Now the problem is that I want this list to be shown as a list of radio buttons so that the user can select a radio button and the address of the selected file can be saved. Also an error need to be shown if the user doesn't select a radio button and clicks next.
I'm using Visual Studio for coding the gui instead of qt creator.
My code so far is:
.hpp file
#pragma once
#include <QWidget>
#include "ui_secondform.h"
#include "thirdform.hpp"
#include <QRegExp>
#include <QDir>
#include <QDebug>
class SecondForm : public QWidget {
Q_OBJECT
public:
SecondForm(QWidget * parent = Q_NULLPTR);
~SecondForm();
QString processText();
signals:
void firstform();
public slots:
void on_pushButton_next2_clicked();
void on_pushButton_back2_clicked();
void on_lineEdit_textChanged(const QString &arg1);
//void onNewTextEntered(const QString &text);
//void on_lineEdit_textChanged(const QString &arg1);
private:
Ui::SecondForm ui;
ThirdForm *third;
QStringList fileList;
};
.cpp file:
#include "secondform.hpp"
#include "firstform.h"
SecondForm::SecondForm(QWidget * parent) : QWidget(parent) {
ui.setupUi(this);
third = new ThirdForm();
// connected to the slot start the main window on the button in the second window
connect(third, &ThirdForm::secondform, this, &SecondForm::show);
//QString dir = processText();
QDir testPath("D://");
testPath.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
fileList = testPath.entryList();
ui.listWidget->addItems(fileList);
}
SecondForm::~SecondForm() {
}
void SecondForm::on_pushButton_back2_clicked() {
this->hide();
emit firstform();
}
void SecondForm::on_pushButton_next2_clicked() {
this->close();
third->show();
}
void SecondForm::on_lineEdit_textChanged(const QString &arg1) {
QRegExp regExp(arg1, Qt::CaseInsensitive, QRegExp::Wildcard);
ui.listWidget->clear();
ui.listWidget->addItems(fileList.filter(regExp));
}
QString SecondForm::processText()
{
FirstForm first;
const QString dir = first.lineEdit()->text();
return dir;
// do something with the text
}
Output:

Unavailable Ui properties

I've been trying to implement a project in C++ using Qt Creator – this is Qt Creator 3.3.1 (opensource) based on Qt Creator 5.4.1. I use Ubuntu 14.04.
I've found some tutorials of which the subject was simillar to what I wanted to create, so I've been studying the code and trying to fit it to my needs. This is the GUI Project. I've been also dealing with this project to learn more C++, OOP. I did 2 forms.
The project consists of 3 classes for now. One class includes a form to gather some information about persons – it works. The main class includes QtableWidget to present the details about the persons from the database in the table, I also implemented the method to find persons (also in the main class), searching by Surnames – in form I used QlineEdit to do it.
But I need another form to edit the information about the strict person. I decided to implement the form to edit the information in another class. There occurred the problem, because to edit the information about the strict person, I need to be able to read what I typed into the gap of QlineEdit and then to make a search in the database using this information (from the QlineEdit which is included in the form of the main class).
The issue is that, in the second class (to edit) when I use a construction in the constructor as: QString Surname = ui->name_of_the_gap->text(); - where ”name_of_the_gap” is the name of the gap which includes the surname that I want to use but it happens to be unavailable for this UI (the ui in the class in which there is this form to edit information).
I have tried to use inheritance but non of it works. Could I please ask you to direct me / point me out how should I do it / what should I change?
Below I will present you the pieces of the code:
addrecord.h
#ifndef ADDRECORD_H
#define ADDRECORD_H
#include <QDialog>
#include <QtSql/qsqldatabase.h>
#include <QtSql/QSqlError>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/qsqldriver.h>
#include <QtSql/QSqlDriverPlugin>
#include <QtSql/qsqldriverplugin.h>
#include <QSqlQuery>
#include <QDebug>
#include <QString>
#include <QMessageBox>
namespace Ui {
class AddRecord;
}
class AddRecord : public QDialog
{
Q_OBJECT
public:
explicit AddRecord(QWidget *parent = 0);
~AddRecord();
private slots:
void on_btnQuit_clicked();
void on_btnAdd_clicked();
private:
Ui::AddRecord *ui;
};
**/*addrecord.h*/**
editrecord.h
#ifndef EDITRECORD_H
#define EDITRECORD_H
#include <QDialog>
//#include "mainwindow.h"
//#include "addrecord.h"
#include <QLineEdit>
namespace Ui {
class EditRecord;
}
class EditRecord : public QDialog
//class EditRecord : public MainWindow
{
Q_OBJECT
public:
explicit EditRecord(QWidget *parent = 0);
~EditRecord();
Ui::EditRecord *eui;
private slots:
void on_btnQuit_clicked();
private:
//Ui::EditRecord *ui;
//Ui::EditRecord *ui;
//Ui::MainWindow *mui;
QLineEdit *searchSurnameEdit;
};
#endif // EDITRECORD_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "addrecord.h"
#include "editrecord.h"
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QSql>
#include <QtSql/qsqldatabase.h>
#include <QtSql/QSqlError>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/qsqldriver.h>
#include <QtSql/QSqlDriverPlugin>
#include <QtSql/qsqldriverplugin.h>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlRecord>
#include <QSqlTableModel>
#include <QModelIndexList>
#include <QTableView>
#include "editrecord.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow *ui;
void fillTable();
private slots:
void on_tableWidget_cellChanged(int row, int column);
void on_btnQuit_clicked();
void on_btnAdd_clicked();
void on_btnSearchSurname_clicked();
void on_btnEditData_clicked();
private:
bool loading;
//Ui::MainWindow *ui;
QStandardItemModel *model;
QSqlDatabase *myDb;
QSqlTableModel *empmodel;
QItemSelectionModel *selection;
QTableView *view;
QModelIndexList indexes;
QSqlQuery *q;
//EditRecord *editrecord;
};
#endif // MAINWINDOW_H
addrecord.cpp
#include "addrecord.h"
#include "ui_addrecord.h"
AddRecord::AddRecord(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddRecord)
{
ui->setupUi(this);
}
AddRecord::~AddRecord()
{
delete ui;
}
void AddRecord::on_btnQuit_clicked()
{
this->close();
}
void AddRecord::on_btnAdd_clicked()
{
QSqlDatabase db1 = QSqlDatabase::addDatabase("QMYSQL");
db1.setHostName("localhost");
db1.setDatabaseName("dbname");
db1.setUserName("user");
db1.setPassword(„passwd");
db1.open();
QString gkUserid,name,second_name,surname,date_of_birth,NIP,street,postalcode,desc,telefhone,mobile_phone,email,sex,city;
name = ui->nameEdit->text();
second_name = ui->secondNameEdit->text();
surname = ui->surnameEdit->text();
date_of_birth = ui->dateofBirthEdit->text();
NIP = ui->nipEdit->text();
street = ui->streetEdit->text();
postalcode = ui->postalCodeEdit->text();
desc = ui->descEdit->acceptRichText();
telefhone = ui->telephoneEdit->text();
mobile_phone = ui->mobilePhoneEdit->text();
email = ui->eMailEdit->text();
sex = ui->sexEdit->text();
city = ui->cityEdit->text();
if(!db1.open()){
qDebug()<<"Failed to open database";
return;
} else {
qDebug()<<"OK";
}
QSqlQuery query("qt_mysql");
query.prepare("INSERT INTO gkUsers VALUES (:gkUserid,:name,:second_name,:surname,:date_of_birth,:NIP,:street,:postal_code,:desc,:telephone,:mobile_phone,:email,:sex,:city)");
query.bindValue(":name",name);
query.bindValue(":second_name",second_name);
query.bindValue(":surname",surname);
query.bindValue(":date_of_birth",date_of_birth);
query.bindValue(":NIP",NIP);
query.bindValue(":street",street);
query.bindValue(":postal_code",postal_code);
query.bindValue(":desc",desc);
query.bindValue(":telephone",telephone);
query.bindValue(":mobile_phone",mobile_phone);
query.bindValue(":email",email);
query.bindValue(":sex",sex);
query.bindValue(":city",city);
if(query.exec()){
QMessageBox::critical(this,tr("Save"),tr("Saved"));
db1.close();
ui->nameEdit->setText("");
ui->secondNameEdit->setText("");
ui->surnameEdit->setText("");
ui->dateofbirthEdit->setText("");
ui->nipEdit->setText("");
ui->streetEdit->setText("");
ui->postalCodeEdit->setText("");
ui->descEdit->acceptRichText();
ui->telephoneEdit->setText("");
ui->mobilephoneEdit->setText("");
ui->eMailEdit->setText("");
ui->sexEdit->setText("");
ui->cityEdit->setText("");
} else {
QMessageBox::critical(this,tr("Error"),query.lastError().text());
}
}
editrecord.cpp
#include "editrecord.h"
#include "ui_editrecord.h"
#include "mainwindow.h"
EditRecord::EditRecord(QWidget *parent):
// MainWindow(),
QDialog(parent),
//eui(new Ui::EditRecord), MainWindow(parent)
eui(new Ui::EditRecord)
{
eui->setupUi(this);
//ui->setupUi(mui->placeholder);
// EditRecord(Ui::MainWindow *ui)
//QString Surname = ui->szukajNazwiskoEdit->text();
// eui->setupUi(ui.placeholder);
}
EditRecord::~EditRecord()
{
delete eui;
}
void EditRecord::on_btnZamknij_clicked()
{
this->close();
}
**/*editrecord.cpp*/**
**/*main.cpp*/**
#include "mainwindow.h"
#include "editrecord.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
EditRecord e; //added
w.show();
return a.exec();
}
**/*main.cpp*/**
**/*mainwindow.cpp*/**
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addrecord.h"
#include <QSql>
#include <QtSql/qsqldatabase.h>
#include <QtSql/QSqlError>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/qsqldriver.h>
#include <QtSql/QSqlDriverPlugin>
#include <QtSql/qsqldriverplugin.h>
#include <QSqlQuery>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase myDb = QSqlDatabase::addDatabase("QMYSQL");
myDb.setHostName("localhost");
myDb.setDatabaseName("db");
myDb.setUserName("user");
myDb.setPassword("passwd");
myDb.open();
qDebug()<<myDb.open();
ui->tableWidget->hideColumn(0);
fillTable();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::fillTable()
{
loading = true;
int num_rows, r, c;
//QSqlQuery q(myDb);
QSqlQuery q;
//get the number of rows
if(!q.exec("SELECT count(gkUserid) as num_rows FROM gkUsers")) qDebug()<< q.lastError().text();
q.first();
num_rows = q.value(0).toInt();
ui->tableWidget->setRowCount(num_rows);
ui->tableWidget->setMaximumWidth(1700);
ui->tableWidget->setMaximumHeight(300);
if(!q.exec("SELECT gkUserid, name, second_name, surname, date_of_birth, NIP, street, postalcode, desc, telephone, mobile_phone, email, sex, city FROM gkUsers ORDER BY gkUserid")) qDebug() << q.lastError().text();
for(r = 0, q.first(); q.isValid(); q.next(), ++r)
{
//for(c = 0; c < q.numRowsAffected(); ++c)
for(c = 0; c < 14; ++c)
{
ui->tableWidget->setItem(r,c, new QTableWidgetItem(q.value(c).toString()));
}
}
loading = false;
}
void MainWindow::on_tableWidget_cellChanged(int row, int column)
{
//int id = ui->tableWidget->item(row, 0)->text().toInt();
if (loading) return;
QSqlQuery q;
q.prepare("UPDATE gkUsers SET name = :i, second_name = :d_i, surname = :n, date_of_birth = :d_u, NIP = :N, street = :u, postal_code = :k, opis = :o, telephone = :t, mobile_phone = :t_k, email = :e, sex = :p, city = :m WHERE gkUserid = :gkUserid");
q.bindValue(":i", ui->tableWidget->item(row, 1)->text());
q.bindValue(":d_i",ui->tableWidget->item(row, 2)->text());
q.bindValue(":n", ui->tableWidget->item(row, 3)->text());
q.bindValue(":d_u", ui->tableWidget->item(row, 4)->text());
q.bindValue(":N", ui->tableWidget->item(row, 5)->text());
q.bindValue(":u", ui->tableWidget->item(row, 6)->text());
q.bindValue(":k", ui->tableWidget->item(row, 7)->text());
q.bindValue(":o", ui->tableWidget->item(row, 8)->text());
q.bindValue(":t", ui->tableWidget->item(row, 9)->text());
q.bindValue(":t_k", ui->tableWidget->item(row, 10)->text());
q.bindValue(":e", ui->tableWidget->item(row, 11)->text());
q.bindValue(":p", ui->tableWidget->item(row, 12)->text());
q.bindValue(":m", ui->tableWidget->item(row, 13)->text());
q.bindValue(":gkUserid", ui->tableWidget->item(row, 0)->text().toInt());
if(!q.exec()) qDebug() << q.lastError().text();
fillTable();
}
void MainWindow::on_btnQuit_clicked()
{
this->close();
}
void MainWindow::on_btnAdd_clicked()
{
//QMainWindow window;
//AddRecord * addrecord = new AddRecord(this);
AddRecord addrecord;
addrecord.setModal(true);
addrecord.exec();
}
void MainWindow::on_btnSearchSurname_clicked()
{
QString Surname = ui->searchSurnameEdit->text();
qDebug()<<Surname;
QSqlDatabase myDb = QSqlDatabase::addDatabase("QMYSQL");
myDb.setHostName("localhost");
myDb.setDatabaseName("db");
myDb.setUserName("user");
myDb.setPassword("passwd");
qDebug()<<myDb.open();
if(!myDb.open()){
qDebug()<<"There is no connection to DB";
return;
}
QSqlQuery qry;
if(qry.exec("SELECT gkUserid, name, second_name, surname, date_of_birth, NIP, street, postal_code, desc, telephone, mobile_phone, email, sex, city FROM gkUsers WHERE surname = \'" + Surname + "\'"))
{
if(qry.next()){
QString msg1 = qry.value(1).toString();
QString msg2 = qry.value(2).toString();
QString msg3 = qry.value(3).toString();
QString msg4 = qry.value(4).toString();
QString msg5 = qry.value(5).toString();
QString msg6 = qry.value(6).toString();
QString msg7 = qry.value(7).toString();
QString msg8 = qry.value(8).toString();
QString msg9 = qry.value(9).toString();
QString msg10 = qry.value(10).toString();
QString msg11 = qry.value(11).toString();
QString msg12 = qry.value(12).toString();
QString msg13 = qry.value(13).toString();
QString msg14 = qry.value(14).toString();
QString msg15 = qry.value(15).toString();
ui->nameEdit->setText(msg1);
ui->surnameEdit->setText(msg3);
ui->dateofbirthEdit->setText(msg4);
ui->nipEdit->setText(msg5);
ui->telEdit->setText(msg9);
ui->sexEdit->setText(msg12);
ui->mobileEdit->setText(msg10);
ui->streetEdit->setText(msg5);
ui->cityEdit->setText(msg13);
ui->descEdit->setText(msg8);
myDb.close();
} else {
qDebug()<<"Something went wrong";
}
}
}
void MainWindow::on_btnEditData_clicked()
{
EditRecord editrecord;
editrecord.setModal(true);
editrecord.exec();
//editrecord = new EditRecord(this);
//editrecord->show();
}
mydelegate.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MyDelegate
TEMPLATE = app
QT += sql
SOURCES += main.cpp\
mainwindow.cpp \
addrecord.cpp \
editrecord.cpp
HEADERS += mainwindow.h \
addrecord.h \
editrecord.h
FORMS += mainwindow.ui \
addrecord.ui \
editrecord.ui \
edit_record.ui
It's because each class has its own Ui which represents the specific UI elements you've defined for that Form and not others. So in EditRecord class you can't access ui->name_of_the_gap, because no such thing is defined in the EditRecord form. The fact that it's been defined for your other class, is irrelevant here, because you have no access to it.
The solution is to get whatever info you need (in your case, the text that has been entered in the QLineEdit of MainWindow) before showing the EditRecord and then pass that value to EditRecord. In other words, you have to get the value from QLineEdit when you can access it and pass that value, instead of trying to access that QLineEdit when you can't.
For doing this you have to pass that value to the constructor of you EditForm. The changes you need are like this:
//In editrecord.h:
explicit EditRecord(QString surname, QWidget *parent = 0);
//In editrecord.cpp:
EditRecord::EditRecord(QString surname, QWidget *parent):
QDialog(parent),
eui(new Ui::EditRecord)
{
eui->setupUi(this);
//Now you have access to 'surname'. Do whatever you need to do with it.
//...
}
//In maitwindow.cpp:
void MainWindow::on_btnEditData_clicked()
{
QString surname = ui->name_of_the_gap->text();
EditRecord editrecord(surname);
editrecord.setModal(true);
editrecord.exec();
}
Note that you can also send the ui this way too, but it's considered bad in OOP.

QT - QInputDialog How to Validate?

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.