Activating particular LineEdit while taking input from on screen numpad - c++

I am making a login form in qt where input is taken from the numpad. The numpad is created on screen. When I am trying to take input from the numpad,
It takes the input in the both of the lineEdit field. So how can I separate both of these while taking the input from the numpad?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainscreen.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->dB1,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB2,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB3,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB4,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB5,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB6,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB7,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB8,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB9,SIGNAL(released()),this,SLOT(display()));
connect(ui->dB0,SIGNAL(released()),this,SLOT(display()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::display()
{
QPushButton *qp=(QPushButton*)sender();
ui->lineEdit->setText(ui->lineEdit->text()+qp->text());
ui->lineEdit_2->setText(ui->lineEdit_2->text()+qp->text());
}
void MainWindow::on_pushButton_clicked()
{
QString username=ui->lineEdit->text();
QString password=ui->lineEdit_2->text();
if(username=="text"&&password=="text")
{
hide();
main_scn=new MainScreen();
main_scn->setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
main_scn->show();
}
}
the UI is here, please check this out

Creating a numpad in that way is inconvenient since it will place the text in both QLineEdit, and usually the username and password are not always the same.
My proposal is to create a Numpad that does not need to be connected by signals, but an event will be sent so that the widget that has the focus.
numpadwidget.h
#ifndef NUMPADWIDGET_H
#define NUMPADWIDGET_H
#include <QApplication>
#include <QGridLayout>
#include <QKeyEvent>
#include <QToolButton>
#include <QWidget>
class NumpadWidget : public QWidget
{
Q_OBJECT
public:
explicit NumpadWidget(QWidget *parent = nullptr) : QWidget(parent)
{
QGridLayout *lay = new QGridLayout(this);
const std::vector<QPair<QString, int>> values{{"1", Qt::Key_1}, {"2", Qt::Key_2}, {"3", Qt::Key_3},
{"4", Qt::Key_4}, {"5", Qt::Key_5}, {"6", Qt::Key_6},
{"7", Qt::Key_7}, {"8", Qt::Key_8}, {"9", Qt::Key_9},
{".", Qt::Key_Colon}, {"0", Qt::Key_0}, {"*", Qt::Key_Asterisk}};
int i = 0;
int j = 0;
for(const QPair<QString, int> & value : values){
QToolButton *button =new QToolButton;
button->setText(value.first);
button->setFixedSize(40,40);
lay->addWidget(button, i, j);
button->setProperty("text", value.first);
button->setProperty("key", value.second);
connect(button, &QToolButton::clicked, this, &NumpadWidget::onClicked);
j++;
if(j % 3 == 0){
j=0;
i++;
}
}
setFixedSize(sizeHint());
}
private slots:
void onClicked()
{
if(QWidget *widget = QApplication::focusWidget()){
QString text = sender()->property("text").toString();
int key = sender()->property("key").toInt();
QKeyEvent * event = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, text);
QCoreApplication::postEvent(widget, event);
}
}
};
#endif // NUMPADWIDGET_H
Then the NumpadWidget is promoted so that it can be used in Qt Designer, in the following link it indicates how to do it
The complete project can be found in the following link.

Related

How to identify the pressed button in Qt C++?

I have 4 buttons on my main window. Each button opens its own window with its own data. How to identify the pressed button to open right window? For example: I press sales button and it opens a window that shows information about ticket sales.
Mainwindow ui
Here is my code from mainwindow h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <sales.h>
#include <theatres.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void button_pressed();
private:
Ui::MainWindow *ui;
sales *s;
theatres *t;
};
#endif // MAINWINDOW_H
And here is my code from mainwindow cpp:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "build/sqlite/sqlite3.h"
#include <QtSql/QSqlDatabase>
#include <QTableView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect((*ui).pushButton,SIGNAL(released()), this, SLOT(button_pressed()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::button_pressed()
{
s = new sales(this);
s -> show();
}
As Andy Newman already answered
the shortest solution is a lambda function
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QHBoxLayout *h_layout = new QHBoxLayout;
centralWidget()->setLayout(h_layout);
for(int c =1; c <= 10; c++)
{
QPushButton *button = new QPushButton(this); // create button
button->setText(QString::number(c)); // set button id
h_layout->addWidget(button); // add a button to the form
// lambda magic
/* connecting a button signal to a lambda function that captures a pointer to a
button and invokes an arbitrary type function. */
connect(button, &QPushButton::clicked, [this, button]() {
pressedButton(button->text());
});
}
}
void MainWindow::pressedButton(const QString &id_button)
{
qDebug("Pressed button: %ls", id_button.utf16());
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "widget.h"
#include "./ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->btn_0,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_1,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_2,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_3,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
}
Widget::~Widget()
{
delete ui;
}
void Widget::SlotButtonClicked()
{
auto sender = this->sender();
if ( sender == ui->btn_0 ) {
// Click btn_0 to open widget0
} else if ( sender == ui->btn_1 ) {
// Click btn_1 to open widget1
} else if ( sender == ui->btn_2 ) {
// Click btn_2 to open widget2
} else if ( sender == ui->btn_3 ) {
// Click btn_3 to open widget3
}
}
If you can use Qt Designer, the best way to do this is to click with button right on the QPushButton (On .ui file in Qt Designer) and click to "Go to Slot", this will create a private slot to this button! In the header file will create the definition, like this:
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
And in the source file (.cpp) will create the "function" clicked pushButton:
void MainWindow::on_pushButton_clicked()
{
}
void MainWindow::on_pushButton_2_clicked()
{
}
void MainWindow::on_pushButton_3_clicked()
{
}
void MainWindow::on_pushButton_4_clicked()
{
}
Inside of the "function" in .cpp, you put the task that you want this button to do, in this case, to open a new window!
When you click "go to slot" in another button, will create another private slot with the respective number (If is the second QPushButton that you create, the private slot will be called by pushButton_2).
The usual way to do this would be to connect the 4 different buttons to 4 different slots. It looks like you are using QtDesigner so that shouldn't be an issue.
If you were generating an array of buttons at run time you'd run into problems and would need a different solution. You could try something like this to pass an array index to the function, for example:
connect(button[x], &QPushButton::clicked, this, [this, x]() { button_pressed(x); });
Or you could do it the Qt way, which would be to call ::setProperty to store data in the button, and then retrieve it from the event, but it's so esoteric that I can't actually remember how to do that...

How to get QString from one window to another window, via pressing a button in a 3rd window

New to C++ and Qt as part of a research project (biology) and have been struggling with presumably some quite simple stuff. I'd really appreciate someone's help.
I'm working with a GUI for a pre-existing programme and I'm trying to transfer a QString variable from the QLineEdit of one of the windows (inputform), to the QLineEdit of a second window (output form).
The bit I'm stuck with is that I need the output form to appear, with it's LineEdit pre-populated, when I click a button on a third window (filedialog).
Problem:
At start up --> two windows appear: filedialog and inputform.
User enters data into inputform's QLineEdit
User presses 'transferButton' on filedialog window
On button press --> outputform appears, with a QLineEdit pre-populated with the user's data (from the inputform).
I assume the problem is of the getter/setter variety and my variable is probably going out of scope, but I've tried following lots of similar examples but can't make it work.
Thanks in advance.
Here's my code:
Main.cpp
#include "filedialog.h"
#include "inputform.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileDialog w;
InputForm w2;
w.show();
w2.show();
return a.exec();
}
filedialog.h
#ifndef FILEDIALOG_H
#define FILEDIALOG_H
#include <QDialog>
namespace Ui {
class FileDialog;
}
class FileDialog : public QDialog
{
Q_OBJECT
public:
explicit FileDialog(QWidget *parent = nullptr);
~FileDialog();
void setFileName();
QString getFileName();
private slots:
void on_transferButton_clicked();
private:
Ui::FileDialog *ui;
QString fileName;
};
#endif // FILEDIALOG_H
filedialog.ccp
#include "filedialog.h"
#include "ui_filedialog.h"
#include "inputform.h"
#include "ui_inputform.h"
#include "outputform.h"
#include "ui_outputform.h"
FileDialog::FileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FileDialog)
{
ui->setupUi(this);
}
FileDialog::~FileDialog()
{
delete ui;
}
void FileDialog::setFileName()
{
InputForm *inputform = new InputForm;
fileName = inputform->ui->inputLineEdit->text();
}
QString FileDialog::getFileName()
{
return fileName;
}
void FileDialog::on_transferButton_clicked()
{
setFileName();
OutPutForm *outputform = new OutPutForm;
outputform->ui->outputLineEdit->setText(getFileName());
outputform->show();
}
inputform.h
#ifndef INPUTFORM_H
#define INPUTFORM_H
#include <QWidget>
namespace Ui {
class InputForm;
}
class InputForm : public QWidget
{
Q_OBJECT
public:
explicit InputForm(QWidget *parent = nullptr);
~InputForm();
Ui::InputForm *ui;
};
#endif // INPUTFORM_H
inputform.ccp
#include "inputform.h"
#include "ui_inputform.h"
InputForm::InputForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputForm)
{
ui->setupUi(this);
}
InputForm::~InputForm()
{
delete ui;
}
outputform.h
#ifndef OUTPUTFORM_H
#define OUTPUTFORM_H
#include <QWidget>
namespace Ui {
class OutPutForm;
}
class OutPutForm : public QWidget
{
Q_OBJECT
public:
explicit OutPutForm(QWidget *parent = nullptr);
~OutPutForm();
Ui::OutPutForm *ui;
};
#endif // OUTPUTFORM_H
outputform.ccp
#include "outputform.h"
#include "ui_outputform.h"
OutPutForm::OutPutForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::OutPutForm)
{
ui->setupUi(this);
}
OutPutForm::~OutPutForm()
{
delete ui;
}
Thank you for your brief pointer.
After some playing around:
Setup mainwindow (or in my case main dialog window). Generate inputform instance, connect button to inputform.
FileDialog::FileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FileDialog)
{
ui->setupUi(this);
InputForm *inputForm = new InputForm;
connect(ui->transferButton,SIGNAL(clicked()),inputForm,SLOT(getLineEditTextFunc()));
inputForm->show();
}
FileDialog::~FileDialog()
{
delete ui;
}
void FileDialog::on_transferButton_clicked()
{
}
Then from the input form:
Define a function to get the input form's LineEdit text (fileName); and then also generate an output form and populate it's LineEdit with the fileName variable.
InputForm::InputForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputForm)
{
ui->setupUi(this);
}
InputForm::~InputForm()
{
delete ui;
}
void InputForm::getLineEditTextFunc()
{
fileName = this->ui->inputLineEdit->text();
OutPutForm *outputform = new OutPutForm;
outputform->ui->outputLineEdit->setText(fileName);
outputform->show();
}

True content is not copied from one QMap into another QMap

When I press the close button on the tab widget, that tab must close, and the corresponding member of the identifyedWidgetMap must be copied into the deletedWidgetMap. However, the copying does not happen.
For instance, I close Tab_3 but there is in the deletedWidgetMap some information: 4, QWidget(0x17424a1).
Help if you can. Thank you.
That code lines I created are located below.
Header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextEdit>
#include <QMap>
#include <QList>
#include <iostream>
#include <QDebug>
#include <QString>
#include <QFont>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void setMainWindowPropertiesAndItems();
QMenuBar* menuBar;
QMenu* fileMenu;
QAction* newTabAction;
QAction* openAction;
QTabWidget* tabWidget;
QMap<int, QWidget*> identifyedWidgetMap;
QMap<int, QWidget*> deletedWidgetMap;
QList<QTextEdit*> textEditList;
QVBoxLayout* vBoxLayout;
private slots:
void newTabActionHandler();
void openActionHandler();
void tryingToCloseConcreteTab(int);
};
#endif // MAINWINDOW_H
Source
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setMainWindowPropertiesAndItems();
}
MainWindow::~MainWindow()
{
}
void MainWindow::setMainWindowPropertiesAndItems()
{
setWindowTitle("Notepad");
setGeometry(0, 0, 500, 250);
move(270, 270);
menuBar = new QMenuBar(this);
setMenuBar(menuBar);
fileMenu = new QMenu("&File", this);
menuBar->addMenu(fileMenu);
newTabAction = new QAction("&New Tab", this);
fileMenu->addAction(newTabAction);
connect(newTabAction, SIGNAL(triggered()), this,
SLOT(newTabActionCommandsHandler()));
openAction = new QAction("&Open", this);
fileMenu->addAction(openAction);
connect(openAction, SIGNAL(triggered()), this,
SLOT(openActionCommandsHandler()));
identifyedWidgetMap.insert(0, new QWidget(this));
textEditList.append(new QTextEdit(this));
tabWidget = new QTabWidget(this);
tabWidget->addTab(identifyedWidgetMap.value(0), QString("Tab
%1").arg(identifyedWidgetMap.size()-1));
tabWidget->setMovable(true);
tabWidget->setTabsClosable(true);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this,
SLOT(tryingToCloseConcreteTab(int)));
vBoxLayout = new QVBoxLayout();
identifyedWidgetMap.value(0)->setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[0]);
textEditList[0]->setCurrentFont(QFont("Monospace Regular", 14));
setCentralWidget(tabWidget);
}
void MainWindow::newTabActionCommandsHandler()
{
if(bool(deletedWidgetMap.isEmpty()) == true)
{
identifyedWidgetMap.insert(identifyedWidgetMap.size(), new
QWidget(this));
textEditList.append(new QTextEdit(this));
tabWidget->
addTab(identifyedWidgetMap.value(identifyedWidgetMap.size()-1),
QString("Tab %1").arg(identifyedWidgetMap.size()-1));
tabWidget->
setCurrentWidget(identifyedWidgetMap.
value(identifyedWidgetMap.size()-1));
vBoxLayout = new QVBoxLayout();
identifyedWidgetMap.value(identifyedWidgetMap.size()-1)->
setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[textEditList.size()-1]);
textEditList[textEditList.size()-1]->setCurrentFont(QFont("Monospace
Regular", 14));
}
}
void MainWindow::openActionCommandsHandler()
{
QWidget* currentWidget = tabWidget->currentWidget();
std::cout<<"The currentWidget address is: "<<currentWidget<<std::endl;
qDebug()<<"The complete identifyedWidgetMap address set is: ";
foreach(int widgetIdentifyer, identifyedWidgetMap.keys())
{
qDebug()<<widgetIdentifyer<<", "
<<identifyedWidgetMap.value(widgetIdentifyer);
}
int currentWidgetIndex = 0;
currentWidgetIndex = tabWidget->currentIndex();
std::cout<<"The currentWidgetIndex is: "<<currentWidgetIndex<<std::endl;
qDebug()<<"The complete deletedWidgetMap set is: ";
foreach(int widgetIdentifyer, deletedWidgetMap.keys())
{
qDebug()<<widgetIdentifyer<<", "
<<deletedWidgetMap.value(widgetIdentifyer);
}
}
void MainWindow::tryingToCloseConcreteTab(int concreteIndex)
{
tabWidget->removeTab(concreteIndex);
std::cout<<"Deleted identifyer is: "<<concreteIndex<<std::endl;
deletedWidgetMap.insert(identifyedWidgetMap.key(tabWidget-
>widget(concreteIndex)),
identifyedWidgetMap.value(identifyedWidgetMap.key(tabWidget-
>widget(concreteIndex))));
}
The problem I see is you first remove the tab from the tabWidget. This results in an index change, the next tab will shift to left when you remove the tab. When you try to access the tab with tabWidget->widget(concreteIndex)you access the next tab.
You may consider adding tab to the deletedWidgetMap before removing it.

QPushButton color change in QLineEdit eventFilter

I would like to make a push button visible when I insert a number in one of the lineedit. The button and linedit are on the same row. I know the position or name of the lineedit but i dont know how to link back to the push button to make it visible or to be able to change color.
If you look in the eventFilter thats where I'm stuck and I need some help.
Thank you
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QRegion>
#include <QLayoutItem>
#include <QList>
#include<QObject>
#include <QEvent>
#include <QKeyEvent>
#include <QModelIndexList>
#include <QKeySequence>
#include <QSignalMapper>
#include<QIntValidator>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
ui->scrollArea->setWidgetResizable( true );
QWidget *widget = new QWidget(this);
ui->scrollArea->setWidget( widget );
QGridLayout *gridLayout = new QGridLayout();
widget->setLayout( gridLayout );
QPushButton *button[20];
for (int i = 0; i < 20; i++)
{
QLabel *label = new QLabel( QString( "%1" ).arg( i ) );
gridLayout->addWidget(label,i,1,1,1 );
gridLayout->setColumnMinimumWidth(1, 100);
gridLayout->setColumnMinimumWidth(2, 10);
if (i==5)
{
QLineEdit *lineEdit = new QLineEdit;
gridLayout->addWidget(lineEdit,i,5,1,1);
} else
{
QLineEdit *lineEdit = new QLineEdit(this);
gridLayout->addWidget(lineEdit,i,3,1,1);
lineEdit->setValidator(new QIntValidator(0,100,this));
lineEdit->setObjectName(QString::number(i));
lineEdit->installEventFilter(this);
gridLayout->setColumnMinimumWidth(4, 25);
gridLayout->setColumnMinimumWidth(5, 50);
gridLayout->setColumnMinimumWidth(6, 25);
QLineEdit *lineEdit_B = new QLineEdit;
gridLayout->addWidget(lineEdit_B,i,7,1,1);
lineEdit_B->setValidator(new QIntValidator(0,100,this));
gridLayout->setColumnMinimumWidth(8, 10);
}
gridLayout->setColumnMinimumWidth(9, 10);
button[i] = new QPushButton();
gridLayout->addWidget(button[i],i,10,1,1);
gridLayout->setColumnStretch(10,20);
button[i]->setFixedHeight(20);
button[i]->setFixedWidth(20);
button[i]->setStyleSheet("background-color:red;");
button[i]->setText(QString::number(i));
QRegion* region = new QRegion(*(new QRect(button[i]->x(),button[i]->y(),15,15)),QRegion::Ellipse);
button[i]->setMask(*region);
button[i]->setVisible(false);
gridLayout->setColumnMinimumWidth(10, 50);
gridLayout->setColumnMinimumWidth(11, 10);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject * obj, QEvent *event)// *event)
{
if (event->type() == QEvent::KeyPress)
{
ui->lineEdit->setText(QString("%1").arg(obj->objectName()));
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug() << "key " << keyEvent->key() << "from" << obj;
// HERE'S WHERE I NEED HELP
QPushButton* button = ui->scrollArea->findChild<QPushButton*>();
// I think here I would use a for loop to match the listedit
// with a number with the corresponding push button.
// When i find the push button I set it to yellow.
}
return QObject::eventFilter(obj, event);
}
void MainWindow::on_pushButton_clicked()
{
}
It is not necessary to use eventFilter, you complicate the task since it is difficult to discriminate which element it is. One possible option is to use the textChanged signal to display the button.
Also you had many tasks that are executed many times in the loop unnecessarily, those tasks that do not depend on the index must be out.
Also if you are going to store buttons do not use arrays, use containers like QList.
Also, you should not create pointers indiscriminately, since it is your responsibility to eliminate them, for example, QRegion is passed by value to setMask(), so it is not necessary to create a pointer.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QScrollArea>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
ui->scrollArea->setWidgetResizable( true );
QWidget *widget = new QWidget(this);
ui->scrollArea->setWidget( widget );
QGridLayout *gridLayout = new QGridLayout(widget);
for(int i=0; i<20; i++){
QLabel *label = new QLabel(QString::number(i));
gridLayout->addWidget(label, i, 1);
QPushButton *button = new QPushButton(QString::number(i));
gridLayout->addWidget(button,i,10,1,1);
button->setFixedSize(20, 20);
button->setStyleSheet("background-color:red;");
QRegion region(QRect(button->pos(),QSize(15,15)),QRegion::Ellipse);
button->setMask(region);
button->hide();
if(i==5){
QLineEdit *lineEdit = new QLineEdit;
gridLayout->addWidget(lineEdit,i,5);
connect(lineEdit, &QLineEdit::textChanged, [button](const QString &text){
button->setVisible(!text.isEmpty());
});
}
else{
QLineEdit *lineEdit_A = new QLineEdit;
gridLayout->addWidget(lineEdit_A,i,3);
lineEdit_A->setValidator(new QIntValidator(0,100,this));
QLineEdit *lineEdit_B = new QLineEdit;
gridLayout->addWidget(lineEdit_B,i, 7);
lineEdit_B->setValidator(new QIntValidator(0,100,this));
connect(lineEdit_A, &QLineEdit::textChanged, [button](const QString &text){
button->setVisible(!text.isEmpty());
});
connect(lineEdit_B, &QLineEdit::textChanged, [button](const QString &text){
button->setVisible(!text.isEmpty());
});
}
}
gridLayout->setColumnMinimumWidth(1, 100);
gridLayout->setColumnMinimumWidth(2, 10);
gridLayout->setColumnMinimumWidth(4, 25);
gridLayout->setColumnMinimumWidth(5, 50);
gridLayout->setColumnMinimumWidth(6, 25);
gridLayout->setColumnMinimumWidth(8, 10);
gridLayout->setColumnMinimumWidth(9, 10);
gridLayout->setColumnStretch(10,20);
gridLayout->setColumnMinimumWidth(10, 50);
gridLayout->setColumnMinimumWidth(11, 10);
}
MainWindow::~MainWindow()
{
delete ui;
}
The complete example can be found in the following link.

Want to add pushbutton to setTimer and setText

I am newbie in GUI design. Here, in sending and receiving messages (float, integer values) using DDS (OpenSplice) I am trying to add a pushButton to my already existing labels(displaying some float values as shown below), so that after a clicking on the pushButton, I should be able to see data in my label.
I tried adding a push button with the help of Qt Network sender Example. Now I get the error undefined reference to 'MainWindow::on_pushButton_clicked() in the file moc_mainwindow.cpp while building the project.
fastsender.cpp
FastSender::FastSender(QLabel *x, QObject *parent) : QObject(parent)
{
wSend = x;
dataTimer = new QTimer(this);
emergency = new QPushButton(tr("Emergency"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(emergency,QDialogButtonBox::ActionRole);
connect(emergency, SIGNAL(clicked()), this, SLOT(startsending()));
connect(dataTimer, SIGNAL(timeout()), this, SLOT(walk()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(buttonBox);
}
void FastSender::startsending()
{
emergency->setEnabled(false);
dataTimer->start(100); // Interval 0 means to refresh as fast as possible
}
int FastSender::walk()
{
msg->value= i+0.1;
snprintf (buf, MAX_MSG_LEN, "Message no. %d", i);
cout << "Writing message: \"" << msg->value << "\"" << endl;
status = talker->write(*msg, userHandle);
QString s= QString::number(msg->value, 'f',8);
wSend->setText(s);
}
fastsender.h
class FastSender : public QObject
{
Q_OBJECT
public:
explicit FastSender(QObject *parent = 0);
FastSender(QLabel *x, QObject *parent = 0);
~FastSender();
signals:
private:
QTimer* dataTimer;
QLabel *wSend;
DDS::DomainParticipantFactory_var dpf;
DDS::DomainParticipant_var parentDP;
DDS::Topic_var signalTopic;
DDS::DataReader_var parentReader;
DDS::DataWriter_var parentWriter;
fw::signalSeq_var msgSeq;
char * signalTypeName;
fw::signalDataWriter_var talker;
fw::signal *msg;
DDS::InstanceHandle_t userHandle;
DDS::Publisher_var fwPublisher;
int alpa;
QPushButton *emergency;
QDialogButtonBox *buttonBox;
public slots:
int walk();
int hello();
void startsending();
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fwdds = new FastWanDDS(ui->label);//receiving values are displayed in label
fwdds1 = new FastSender(ui->label_2);//Sending values are displayed in label_2
}
mainwindow.h
Ui::MainWindow *ui;
QTimer* timer;
int counter;
FastWanDDS *fwdds;
FastSender *fwdds1;
Any Help Appreciated.
Note: Only snippets of the code presented
I got it to work this way. Does this work for you?
mainwindow.cpp
#include "mainwindow.h"
#include "fastsender.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QLabel* label = new QLabel("unchanged");
FastSender* fs = new FastSender(label);
setCentralWidget(fs);
}
MainWindow::~MainWindow()
{
}
fastsender.h
#ifndef FASTSENDER_H
#define FASTSENDER_H
#include <QLabel>
#include <QTimer>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>
class FastSender : public QWidget
{
Q_OBJECT
public:
FastSender(QLabel* label, QWidget* parent = 0)
: QWidget(parent)
, _label(label)
, _timer(new QTimer)
, _emergency(new QPushButton(tr("Emergency")))
, _bbox(new QDialogButtonBox)
{
_bbox->addButton(_emergency, QDialogButtonBox::ActionRole);
QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(_label);
layout->addWidget(_bbox);
setLayout(layout);
connect(_emergency, SIGNAL(clicked(bool)), this, SLOT(startsending()));
connect(_timer, SIGNAL(timeout()), this, SLOT(walk()));
}
public slots:
void startsending()
{
_emergency->setEnabled(false);
_timer->start(100);
}
int walk()
{
_label->setText("changed");
_emergency->setEnabled(true);
}
private:
QLabel* _label;
QTimer* _timer;
QPushButton* _emergency;
QDialogButtonBox* _bbox;
};
#endif // FASTSENDER_H