Results 1 to 11 of 11

Thread: QlineEdit and mouse clicks

  1. #1
    Join Date
    Apr 2021
    Location
    Austria
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default QlineEdit and mouse clicks

    Some years ago I implemented a Sudoku checker and solver on Windows XP/Qt 4.8.1/Visual Studio 8. Recently I reactivated the code on Xindows 8/Qt 5.13.1/Visual Studio 2017. The application is working fine but a strange effect I already noticed on the old system is still there.

    The main Sudoku panel, meaning the fields for the 81 digits I realized using 81 QLineEdit with setMaxLength(1) and setInputMask("D"). I can display loaded or calculated Sudokus and I can also input digits when defining or solving ones.

    The strange thing I would like to understand and failed to find in documentation or other threads happens when I do a mouse click to select the QLineEdit where I want to input a digit.
    There are two sensitive points in the QLineEdit although MaxLength==1.
    When I click in the center of the field I get a cursur in the center that takes about a third of the place and no input is accepted.
    When I click on the left third of the field I get a cursor in the left third and I have all the functionality I want.

    If somebody could explain why there are two separate sensitive points and what I could do to get rid of the unusable one of the two that would be really great.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QlineEdit and mouse clicks

    Without seeing some code or a .ui file, there isn't enough information here to give an answer. From your description, it sounds like you have two widgets overlapped so depending where you click you set focus to one or the other of them.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2021
    Location
    Austria
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QlineEdit and mouse clicks

    Basically it is something like:

    Qt Code:
    1. for (int y = 0; y<9; ++y)
    2. {
    3. for (int x = 0; x<9; ++x)
    4. {
    5. textEditSudo[x][y] = new QLineEdit;
    6. }
    7. }
    8.  
    9. for (int y = 0; y<9; ++y)
    10. {
    11. for (int x = 0; x<9; ++x)
    12. {
    13. textEditSudo[x][y]->setFont(font);
    14. textEditSudo[x][y]->setReadOnly(false);
    15. textEditSudo[x][y]->setMaximumWidth(36);
    16. textEditSudo[x][y]->setMaximumHeight(36);
    17. textEditSudo[x][y]->setMinimumWidth(36);
    18. textEditSudo[x][y]->setMinimumHeight(36);
    19. textEditSudo[x][y]->setMaxLength(1);
    20. textEditSudo[x][y]->setInputMask("D"); // numbers 1..9
    21. textEditSudo[x][y]->insert(tmpStr.setNum(x+1)); // display test only
    22. textEditSudo[x][y]->setContextMenuPolicy(Qt::NoContextMenu);
    23. }
    24. }
    25.  
    26. for (int y = 0; y<9; ++y)
    27. {
    28. for (int x = 0; x<9; ++x)
    29. {
    30. connect(textEditSudo[x][y], SIGNAL(textEdited(const QString &)), this, SLOT(getTextEditSudoChanged(const QString &)));
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 
    But although I don't think it can be a second widget interfering it might be a good idea to make a small program with only these basic widgets, or even only a single one, to check the behaviour then.
    Or is there anything in the above code snippet that must not be done that way?
    Last edited by d_stranz; 15th April 2021 at 22:04. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QlineEdit and mouse clicks

    So, are you putting these into a grid layout? How are you positioning them in your main widget?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Apr 2021
    Location
    Austria
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QlineEdit and mouse clicks

    I tried to put together all parts that might be relevant and leave out all parts of the board that are not relevant:

    Qt Code:
    1. QGroupBox *gridGroupBoxDA = new QGroupBox(tr("Display Area"));
    2. QGridLayout *gridLayoutDA = new QGridLayout;
    3.  
    4. QGridLayout *gridLayoutDA1 = new QGridLayout;
    5. for (int y = 0; y < 9; ++y)
    6. {
    7. for (int x = 0; x < 9; ++x)
    8. {
    9. gridLayoutDA1->addWidget(m_pBoardWidget->getSudoTextWidget(x,y), y, x);
    10. }
    11. }
    12. gridGroupBoxDA1->setLayout(gridLayoutDA1);
    13. gridLayoutDA->addWidget((QWidget *)gridGroupBoxDA1, 0, 0);
    14.  
    15. gridGroupBoxDA->setLayout(gridLayoutDA);
    16.  
    17. QVBoxLayout *mainLayout = new QVBoxLayout;
    18. mainLayout->addWidget(gridGroupBoxDA);
    19.  
    20. QWidget *widget = new QWidget;
    21. widget->setLayout(mainLayout);
    22.  
    23. setCentralWidget(widget);
    To copy to clipboard, switch view to plain text mode 
    Maybe I did make it more complicated than needed but there are lots of other parts on the board I left out here like the second grid (DA2) showing all moves that are possible at the moment etc. I think there is no misleading by leaving these things out because all the widgets I left out here can not overlap since I can see and use them as should be.
    Last edited by d_stranz; 16th April 2021 at 17:43.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QlineEdit and mouse clicks

    Please use CODE tags the next time you post source code. The instructions are in my signature.

    Maybe I did make it more complicated than needed
    You do seem to like running through those double for() loops a lot. Everything you have posted so far could have been done in one set of loops instead of 5 or 6...

    I don't see anything that pops out as being obviously wrong with what you have done. If I have time, I will try to recreate this over the weekend to see if I see similar behavior.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QlineEdit and mouse clicks

    What I think you have here is an insert cursor. There are three possibilities:
    1. You click right of center and the cursor is placed right of the existing character. The max length stops you typing another character.
    2. You click left of center and the cursor is placed before the existing character. The max length will stop you inserting another character.
    3. You click and it selects the character which will be erased when you type another character.


    If you use a QLineEdit subclass that selects all text when focus moves into the widget then you should get the overwriting behaviour (3) by default.
    Qt Code:
    1. #ifndef OVERWRITINGEDIT_H
    2. #define OVERWRITINGEDIT_H
    3.  
    4. #include <QLineEdit>
    5.  
    6. class OverwritingEdit : public QLineEdit
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit OverwritingEdit(QWidget *parent = nullptr);
    11.  
    12. signals:
    13.  
    14.  
    15. // QWidget interface
    16. protected:
    17. void focusInEvent(QFocusEvent *event);
    18. };
    19.  
    20. #endif // OVERWRITINGEDIT_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "overwritingedit.h"
    2.  
    3. OverwritingEdit::OverwritingEdit(QWidget *parent)
    4. : QLineEdit(parent)
    5. {
    6.  
    7. }
    8.  
    9. void OverwritingEdit::focusInEvent(QFocusEvent *event)
    10. {
    11. QLineEdit::focusInEvent(event);
    12.  
    13. // Select all text
    14. selectAll();
    15. }
    To copy to clipboard, switch view to plain text mode 

    This will still not handle, for example, pressing 5 then 4 and expecting to get 4. Pressing 5 leaves the insert cursor after the 5 and you are back into scenario (1) above. Perhaps you could select all in a slot attached to textChanged() or perhaps you are better off with a completely custom widget.

  8. The following user says thank you to ChrisW67 for this useful post:

    d_stranz (18th April 2021)

  9. #8
    Join Date
    Apr 2021
    Location
    Austria
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QlineEdit and mouse clicks

    What I think you have here is an insert cursor. There are three possibilities:
    You click right of center and the cursor is placed right of the existing character. The max length stops you typing another character.
    You click left of center and the cursor is placed before the existing character. The max length will stop you inserting another character.
    You click and it selects the character which will be erased when you type another character.
    Looks like it is really points 1 and 3 happening. As the fastest possible verification test I just inserted selectAll in the receiving slot (or whatever it is called correctly). If I click right it now just selects the digit. If I click left that's what it did before and still does. And if I input a digit it stays selected (and inputable).

    So I now have a quick and a clean way to solve the problem

    Many thanks!

  10. #9
    Join Date
    Apr 2021
    Location
    Austria
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QlineEdit and mouse clicks

    I have to correct myself. I consider the only way that worked not as clean. The selectAll only worked reliable in the following way:

    Qt Code:
    1. QTimer::singleShot(0, [this](){selectAll();});
    To copy to clipboard, switch view to plain text mode 

    This was not my invention but as I started to get a bit desperate I found this solution in a thread about problems with selectAll.

  11. #10

    Default Re: QlineEdit and mouse clicks

    Thank you for your answer, it helped me a lot!

  12. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QlineEdit and mouse clicks

    Quote Originally Posted by GerJan View Post
    I have to correct myself. I consider the only way that worked not as clean.
    I was thinking something like this (untested):
    Qt Code:
    1. #include "overwritingedit.h"
    2.  
    3. OverwritingEdit::OverwritingEdit(QWidget *parent)
    4. : QLineEdit(parent)
    5. {
    6. // The extra parameter coming out of the signal should be ignored by Qt for the purposes of this slot
    7. connect(this, SIGNAL(textChanged(QString)), this, SLOT(selectAll()));
    8. }
    9.  
    10. void OverwritingEdit::focusInEvent(QFocusEvent *event)
    11. {
    12. QLineEdit::focusInEvent(event);
    13.  
    14. // Select all text
    15. selectAll();
    16. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Simulating mouse clicks
    By bvox in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2017, 19:42
  2. Replies: 3
    Last Post: 22nd February 2013, 20:56
  3. Replies: 3
    Last Post: 12th May 2010, 14:11
  4. Generating Mouse Clicks
    By Ebonair in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2009, 19:20
  5. How to handle double clicks with mouse?
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 14th January 2007, 13:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.