Waiting till the QLineEdit text changes - c++

I want to use QLineEdit to get an integer value that I want to work with. My problem is that I want to wait till the text is entered. It would also be nice if I can give a default text at the begining that will automatically be deleted after clicking on the QEditLine, like :
for the first point I tried this and it didn't work:
......
int num =0;
QLineEdit *qtest = new QLineEdit();
........
mailayout->addWiget(qtest);// when I use the while loop the QLineEdit won't be added !!
while(num ==0 ){
num = qtest->text.toInt();
}
.............
the program stays in the while loop, any Idea I'm doing wrong?

Use setPlaceholderTest(const QString&) for text to show when the user has not entered anything.
Don't poll the QLineEdit for changes, this is Qt so use signals.
connect( qtest, SIGNAL( editingFinished() ),
someContainerObj, SLOT( myLineEditSlot() ) );
...
ContainerObj::myLineEditSlot()
{
int num = qtest->text().toInt();
...
}

Related

Qt C++ SLOT on_returnPressed on_OK_ckicked strange behavior

I have a strange behaviour with one of my ui`s:
In my ui I have a lineEdit Shift_x and a Button pb_OK. When I do the following I have different behaviour:
Edit + RETURN:
When I edit the value in Shift_x and press return the slot on_lne_Shift_x_returnPressed() is executed. This is what I would expect. But after that the slot on_pb_OK_clicked() is also executed which in the end makes my program on_MoveCurve_ValueEdited to be executed twice.
Edit + OK: If I edit the value in Shift_x and click “OK” only the slot on_pb_OK_clicked() is executed ( which then starts on_lne_Shift_x_returnPressed(); where the actual shift is triggered)
Why would the program run on_lne_Shift_x_returnPressed() and on_pb_OK_clicked() when I only press the return button?
Thanks for your help!
This is my code:
2 slots:
void MoveSeries::on_lne_Shift_x_returnPressed()
{
const QString arg1= ui->lne_Shift_x->text();
shift_tracker = shift_tracker-arg1.toDouble();
emit x_returnPressed(arg1, ChartSeries);
}
void MoveSeries::on_pb_OK_clicked()
{ on_lne_Shift_x_returnPressed(); }
And a CONNECT in my other class
connect( MOVE_SERIES , SIGNAL( x_returnPressed( QString , QVector < Series*> )),
this,SLOT( on_MoveCurve_ValueEdited ( QString,QVector < Series*>))) ;

Qt c++ Increasing integer with pushButton to label

I'm trying to make a simple "Cookie Clicker" game and I'm having trouble with this. When I press the button I want the label to print out "You have mined (VALUE) FSCoins" but the label won't update for some reason. Console shows no errors :(
Here's my code:
mainwindow.cpp
void MainWindow::on_pushButton_clicked(int num, int numplus)
{
num = numplus + 1;
QString qstr = QString::number(numplus);
ui->label->setText("You have mined " + qstr + " FSCoins");
}
Any help would be appreciated, I've only started working with Qt yesterday and I'm "Sort of" getting the hang of it.
num = numplus + 1;
What is the point of this line? num is a local variable that is never used. Did you mean to pass it by reference?
You need to connect a SIGNAL to a SLOT, but that function you wrote there does not seem like a SLOT. Somewhere in your code there shall be something like this in your header file:
class ...
{
// ...
private slots:
void onPushButtonClicked();
};
and in your source file:
// For example in the constructor.
connect( ui->PushButton, SIGNAL( clicked() ), this, SLOT( onPushButtonClicked() ) );
// The implementation of your SLOT.
Class::onPushButtonClicked()
{
// Your implementation.
updateLabel( /* Your arguments */ );
}
The SLOT function cannot have more arguments than the SIGNAL, so in this case your SLOT cannot have any.
And something else. I prefer this version of creating a QString:
QString( "You have mined %1 coins" ).arg( value );
I think it's more readable.
So the point is that that you need store that integers somewhere. Maybe in your class as a member variable.

Connecting signal returning object to slot without arguments

I have QTablewidget with QDateEdit widget in every row. I want to know which widget was edited by the user. What I did worked in similar situation with QComboBox widgets:
qint32 row = 0;
mapper = new QSignalMapper(this); //QFrame
for (Object const& o : o_list) {
tableWidget->setRowCount(row + 1);
QDateEdit * date = new QDateEdit(o.date); //QDate
date->setProperty(r, row); //const * char('r')
connect(date, &QDateEdit::dateChanged, mapper, &QSignalMapper::map);
mapper->setMapping(date, row);
tableWidget->setCellWidget(row, 0, date);
i++;
}
connect(mapper, SIGNAL(mapped(int)), this, SLOT(myon_dateEdit_dateChanged(int)));
The only problem is that I can't connect QDateEdit::dateChanged(QDate) with QSignalMapper::map() because it doesn't want any argument (I had no problem like this for QComboBox as there is argumentless signal). I don't need QDate argument because while I know the position of the widget - I can check the date later. Any fast solution to this?
Edit: I have QFrame::eventFilter installed on the QDateEdit anyway because I need to change row selection for QTableWidget if QDateEdit was edited so I thought I could use it instead... but QEvent::ModifiedChange doesn't work for that and I don't know what to use...
I remember doing something a while back w/ a signalMapper, but in the end I prefer this:
connect( date, SIGNAL( dateChanged( int ) ), this, SLOT( handleDateChange( int ) ) );
void myObject::handleDateChanged( int ) {
QDateEdit* changedEdit = static_cast<QDateEdit*>( sender() );
// changedEdit is the one that changed
}
You will need to keep track of which QDateEdit is which. I'm usually keeping my widgets in some sort of container anyway for other reasons, so doing the above results in much less code complexity for me.

QProgressBar hanging at 98%

I'm a beginner in Qt and I'm trying to show process bar for my application’s gui . i have written following code
void my_app::progress()
{
int val=1;
while(process.state()!=0)
{
ui->progressBar->setValue(val++);
}
ui->progressBar->setValue(100);
}
but progress bar is hanging at 98% . Can any one suggest me some solution .
thank you !
Two possible points: QProcess:::state() does not enter the Qt event loop - so calling it repeatedly will not update your application; all your Qt logic hangs. And verify the progressBar initialization: If the progressBar property "maximum" is not exactly 100 it'll will not reach 100%. Your "val" also ignores the "progressBar" maximum, so it needs a fix:
if( val < ui->progressBar->maximum() )
ui->progressBar->setValue(val++);
Most jobs have a clear concept of progress, which is not obvious here. Your while loop could be too fast to visually recognize the progress steps. Consider substituting the while loop with a signal/slot pair to sync the progressBar. This will also allow Qt to enter the event loop at times to update your app. Consider using the signal QProcess::readyReadStandardOutput() if your called process outputs something reasonable to stdout. Maybe your process does something, ie fills a buffer, whose size you can use to indicate progress - or simply outputs a count to stdout. Let me sketch here:
.. Somewhere in your my_app constructor ...
.. ui->progressBar->setRange(0, 100); ..
.. connect( this, SIGNAL(processProgressSignal(int)), this, SLOT(progressSlot(int) ); ..
.. connect( &process, SIGNAL(readyReadStandardError()), this, SLOT(processProgress()) ); ..
Q_SIGNALS:
void processProgressSignal( int val );
Q_SLOTS:
void my_app::processProgress()
{
int val = 1;
// Insert your process progress calculation here
// Example: Parse the result of QByteArray QProcess::readAllStandardOutput()
// Example: val = ( buffer.currLength / buffer.maxLength ) * 100;
Q_EMIT processProgressSignal( val );
}
void my_app::progressSlot(int val)
{
if( val < ui->progressBar->maximum() )
ui->progressBar->setValue(val++);
}
If that does not help, make sure that process.state() is 100 times "true" - to allow 100 iterations of your loop. qDebug() is your friend !
Good Luck!

Qt4 existing slots are not recognized

I am currently trying to complete a project using Qt4 and C++. I am using buttons to switch between states. While trying to connect the buttons' clicked() signals to the textEdit to display the relevant state, I got stuck on an error:
Object::connect No such slot
QTextEdit::append("move state")
Object::connect No such slot
QTextEdit::append("link state")
Only, QTextEdit definitely has an append(QString) slot.
Any ideas?
Some code samples:
QPushButton *move = new QPushButton("Move");
connect(move, SIGNAL(clicked()), textEdit, SLOT(append("move state")));
You can't pass in an argument (literally) to the append() slot when making a signal to slot connection.
You refer to it like a method signature:
SLOT(append(QString)) //or const QString, I'm not sure
If you need the textbox to append the words "move state" every time that button is clicked, then you should define your own slot that will do the append.
Chris has it in a nutshell.
That is one of the many reasons I like boost::signals a lot more (you are allowed to use boost::bind). You are basically going to need to create another function that captures the signal and then performs the append.
...
QPushButton *move = new QPushButton("Move");
connect(move, SIGNAL(clicked()), textEdit, SLOT(MoveState()));
}
...
void MyTextEdit::MoveState()
{
append("move state");
}
Use a QSignalMapper to pass a hard-coded argument to the text edit's slot.
Example:
QSignalMapper* signalMapper = new QSignalMapper(this);
QPushButton* move = new QPushButton("Move");
signalMapper->setMapping(move, QString("move state"));
connect(move, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(signalMapper, SIGNAL(mapped(QString)), textEdit, SLOT(append(QString)));
Beware of the bugs in the above code.
Assuming you will have other QPushButtons that will cause other states to occur, you could put them inside a QButtonGroup. Then, you can use an enumeration, such as { MOVE_ID, STOP_ID, ... } to refer to the possible states.
QPushButton* move = new QPushButton( "Move" ) ;
QPushButton* stop = new QPushButton( "Stop" ) ;
QButtonGroup* buttonGroup = new QButtonGroup() ;
buttonGroup->addButton( move, MOVE_ID ) ;
buttonGroup->addButton( stop, STOP_ID ) ;
// Connecting QButtonGroup to writing function
connect( buttonGroup, SIGNAL( buttonClicked( int ) ),
textEdit, SLOT( append( int ) ) ) ;
In textEdit, you'll define a function that appends the appropriate text depending on the state in which you get.
void append( int i )
{
switch ( i )
{
case MOVE_ID:
textEdit->append( "move state" ) ;
break ;
case STOP_ID:
textEdit->append( "stop state" ) ;
break ;
}
}