putting the cursor at the beginning QLineedit
-
Hey guys. How can i always put the cursor at the beginning when i click at my QLineedit? set->cursorposition(0) does not work. Because when i click at it, it can start from the middle of it thats not good.
QLineEdit* dec = new QLineEdit(this); dec->setInputMask("999999999"); dec->setCursorPosition(0); //it does not work
-
Hi,
That's something you could do through focusInEvent.
However, this behaviour will likely surprise your users in the wrong way.
-
Do you mean i will suprise the users with focusInEvent like i do now? How would u do that. I need Qlineedit one where i can only input decimal numbers and one with only binary numbers. I dont understand how to start with focusInEvent.
@thomasGl Now you posted two line edits and it looks like you want to move the cursor to the second one if the user clicks on the first. That is not what you asked first. What is really your use case?
Surprising for the user would be if he/she clicks in a line edit and the cursor is always at the beginning and not where the user actually clicked.
-
I always want to start from the beginning. When I'm in the first object and then click the second object, I always want to start from the beginning. It doesn't matter whether I click from the second into the first object or from the first into the second. It's supposed to be just like the unmodified QLineedit, so it automatically starts from the beginning when I click on it. It can't start from the center unless I move the cursor with the keyboard. Its because of setInputmask. My first question was correct, I just expanded the questions because I don't understand how to use FocusInEvent for that. I use setInputmask here because I want qline1 to get only decimalnumbers and qline2 only binarynumbers
-
I always want to start from the beginning. When I'm in the first object and then click the second object, I always want to start from the beginning. It doesn't matter whether I click from the second into the first object or from the first into the second. It's supposed to be just like the unmodified QLineedit, so it automatically starts from the beginning when I click on it. It can't start from the center unless I move the cursor with the keyboard. Its because of setInputmask. My first question was correct, I just expanded the questions because I don't understand how to use FocusInEvent for that. I use setInputmask here because I want qline1 to get only decimalnumbers and qline2 only binarynumbers
-
-
class qlain: public QLineEdit { public: qlain(QWidget* parent); virtual void focusInEvent(QFocusEvent* )override; }; void qlain::focusInEvent(QFocusEvent *) { this->setCursorPosition(0); }
Like that? now i need to call that function somehow right?
@thomasGl
Assuming code is correct --- which from a glance I think it (mostly) is --- then, no, you do not "need to call that function somehow right?". Because it is anoverride
offocusInEvent()
the whole point is that it will get called automatically whenever your widget gets focus, however that occurs.For an event you ought always call the base class. I think you should set your focus after that, in case base implementation sets cursor, so:
void qlain::focusInEvent(QFocusEvent *event) { QLineEdit::focusInEvent(event); this->setCursorPosition(0); }
-
#ifndef QLAIN_H #define QLAIN_H #include <QLineEdit> #include <QFocusEvent> class qlain: public QLineEdit { public: qlain(QWidget* parent); virtual void focusInEvent(QFocusEvent* event)override; }; #endif // QLAIN_H
#include "qlain.h" qlain::qlain(QWidget *parent):QLineEdit(parent) { } void qlain::focusInEvent(QFocusEvent *event) { QLineEdit::focusInEvent(event); this->setCursorPosition(0); }
#ifndef WindMain_H #define WindMain_H #include <QMainWindow> #include <QLineEdit> #include <qlain.h> class WindMain : public QMainWindow { Q_OBJECT public: WindMain(QWidget *parent = 0); qlain* dec = new qlain(this); //<----- that needs to start always from the beginning. }; #endif // WindMain_H
#include "WindMain.h" WindMain::WindMain(QWidget *parent):QMainWindow(parent) { dec->setInputMask("999999999"); //<---that one }
Mhhhh doesn't work. here my complete code.
-
#ifndef QLAIN_H #define QLAIN_H #include <QLineEdit> #include <QFocusEvent> class qlain: public QLineEdit { public: qlain(QWidget* parent); virtual void focusInEvent(QFocusEvent* event)override; }; #endif // QLAIN_H
#include "qlain.h" qlain::qlain(QWidget *parent):QLineEdit(parent) { } void qlain::focusInEvent(QFocusEvent *event) { QLineEdit::focusInEvent(event); this->setCursorPosition(0); }
#ifndef WindMain_H #define WindMain_H #include <QMainWindow> #include <QLineEdit> #include <qlain.h> class WindMain : public QMainWindow { Q_OBJECT public: WindMain(QWidget *parent = 0); qlain* dec = new qlain(this); //<----- that needs to start always from the beginning. }; #endif // WindMain_H
#include "WindMain.h" WindMain::WindMain(QWidget *parent):QMainWindow(parent) { dec->setInputMask("999999999"); //<---that one }
Mhhhh doesn't work. here my complete code.
@thomasGl
Under Qt5.15, Ubuntu, it "works" for me. When I run the program I see:
Note there is a "heavy/bold cursor" at the left-hand side of the line edit. This must be something to do with the input mask, because I don't usually see such a cursor. At this point I can type digits, and they appear at the left. But if instead I click into the line edit while it is empty the cursor jumps to the middle, and then I don't seem to be able to type anything into it. But I can delete space characters (9 of them present?) and then type. That must all be to do with the input mask.....
-
Hello. I am trying to put the InputMask inside a Qstring variable, but it does not work. How can i do that?
dec->setInputMask("999999999"); //my input for example: 23453 QString tempString = bin->text(); //i want 23453 inside tempstring. That does not work. QString tempString = bin->inputMask(); //does not work either, here i am getting 999999999 as a ouput.
-
Hello. I am trying to put the InputMask inside a Qstring variable, but it does not work. How can i do that?
dec->setInputMask("999999999"); //my input for example: 23453 QString tempString = bin->text(); //i want 23453 inside tempstring. That does not work. QString tempString = bin->inputMask(); //does not work either, here i am getting 999999999 as a ouput.
-
Hello. I am trying to put the InputMask inside a Qstring variable, but it does not work. How can i do that?
dec->setInputMask("999999999"); //my input for example: 23453 QString tempString = bin->text(); //i want 23453 inside tempstring. That does not work. QString tempString = bin->inputMask(); //does not work either, here i am getting 999999999 as a ouput.
-
@thomasGl said in putting the cursor at the beginning QLineedit:
QString tempString = dec->text();
qDebug()<<tempString;Where and when do you execute this code?
-
@thomasGl said in putting the cursor at the beginning QLineedit:
It doesn't show that string.
It will show that string if you call it when that string is in there. If when you call it that string is not in there, it cannot guess what you are going to type and return it before you do so....