Results 1 to 11 of 11

Thread: Focus on a QWidget item in a QWidget

  1. #1
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Focus on a QWidget item in a QWidget

    I can't believe no one has asked about this (or at least that I couldn't find it)... but let's see if someone can give me an answer

    I have a QDialog with a QFrame on it (and a QDialogButtonBox, and a few other widgets). When I try to place a QWidget "in" the frame (setParent(frame)) and show it (note: under Windows I have to have a move(0,0) for the widget or else the widgets start sliding down and to the right until it hits a certain point, then resets), the widgets on the widget (i.e. a QLineEdit on the 'page') do not get the focus. I've tried page->setFocus() -- but that just allows the QDialogButtonBox to have the default button work (weird, but it doesn't work (under Linux) until I added the setFocus()). I've tried calling a function in the page to set the focus to the QLineEdit (m_name->setFocus())... still nothing.

    Can anyone see an obvious solution? Or do I need to try to get a small compilable example to post first?

    Thanks,

    Vycke

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    Better post an example and I will answer the post by correcting it. It is easier that way.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    I'm not sure if it's going to help with the focus problem, but it sounds like you forgot to add a layout for that frame.

  4. #4
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    Ok. I replaced the QFrame with a QVBoxLayout & am doing an insertWidget(0, page) (instead of just making the QFrame the parent of the page) -- even with the page->setFocus() call, it won't show.

    I'm trying to make a small example to test this, but any other possible hints while I try to trim it down would be nice

    Vycke

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    Maybe you set QWidget::focusPolicy to Qt::NoFocus somewhere?

  6. #6
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    (You'll have to put this into an app... qtest.ui is just a dialog (named QTestDlg) w/a QVBoxLayout, qname.ui is a widget (named QNameDlg) w/a QLineEdit)

    testdlg.hpp
    Qt Code:
    1. #include "ui_qtest.hpp"
    2.  
    3. class CTestDlg : public QDialog, private Ui::QTestDlg
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. CTestDlg(QWidget* parent = 0);
    9. ~CTestDlg();
    10.  
    11. void addPage(QWidget* page);
    12. };
    To copy to clipboard, switch view to plain text mode 

    testdlg.cpp
    Qt Code:
    1. #include "testdlg.hpp"
    2.  
    3. CTestDlg::CTestDlg(QWidget* parent) : QDialog(parent)
    4. {
    5. this->setupUi(this);
    6. }
    7.  
    8. CTestDlg::~CTestDlg()
    9. {
    10. }
    11.  
    12. void CTestDlg::addPage(QWidget* page)
    13. {
    14. if (page)
    15. {
    16. vboxLayout->insertWidget(0, page);
    17. page->show();
    18. page->setFocus(Qt::OtherFocusReason);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    and calling the above with

    Qt Code:
    1. CTestDlg* test = new CTestDlg(this);
    2. CNameDlg* page = new CNameDlg();
    3.  
    4. test->addPage(page);
    5. test->show();
    To copy to clipboard, switch view to plain text mode 

    [I hope this is enough to show what I have -- and will give some idea what I'm doing & why it isn't setting the focus on the QLineEdit]

    Vycke
    Last edited by vycke; 3rd January 2008 at 15:04.

  7. #7
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    Quote Originally Posted by jacek View Post
    Maybe you set QWidget::focusPolicy to Qt::NoFocus somewhere?
    The widgets/dialog both had Qt::NoFocus set -- but changing both to Qt::StrongFocus didn't fix this.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    Please post a compilable example.

  9. #9
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Post Re: Focus on a QWidget item in a QWidget

    Ok.. Here's the code:

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2.  
    3. #include "dialog.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. Dialog dialog;
    9. CNameDlg test;
    10. dialog.addPage(&test);
    11. return dialog.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "dialog.h"
    4.  
    5. Dialog::Dialog()
    6. {
    7. this->setupUi(this);
    8. }
    9.  
    10. void Dialog::addPage(QWidget* page)
    11. {
    12. if (page)
    13. {
    14. vboxLayout->insertWidget(0, page);
    15. page->show();
    16. page->setFocus(Qt::OtherFocusReason);
    17. }
    18. }
    19.  
    20. CNameDlg::CNameDlg(QWidget* parent) : QWidget(parent)
    21. {
    22. this->setupUi(this);
    23. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QtGui/QDialog>
    5. #include <QtGui/QLabel>
    6. #include <QtGui/QLineEdit>
    7. #include <QtGui/QVBoxLayout>
    8. #include <QtGui/QWidget>
    9.  
    10. #include <QtGui/QApplication>
    11.  
    12. class Dialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Dialog();
    18. void addPage(QWidget* page);
    19.  
    20. private:
    21. QWidget *verticalLayout;
    22. QVBoxLayout *vboxLayout;
    23. QLabel *label;
    24.  
    25. void setupUi(QDialog *Dialog)
    26. {
    27. if (Dialog->objectName().isEmpty())
    28. Dialog->setObjectName(QString::fromUtf8("Dialog"));
    29. Dialog->resize(535, 302);
    30. Dialog->setFocusPolicy(Qt::StrongFocus);
    31. verticalLayout = new QWidget(Dialog);
    32. verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    33. verticalLayout->setGeometry(QRect(10, 30, 511, 221));
    34. vboxLayout = new QVBoxLayout(verticalLayout);
    35. vboxLayout->setObjectName(QString::fromUtf8("vboxLayout"));
    36. vboxLayout->setContentsMargins(0, 0, 0, 0);
    37. label = new QLabel(Dialog);
    38. label->setObjectName(QString::fromUtf8("label"));
    39. label->setGeometry(QRect(10, 10, 54, 18));
    40.  
    41. Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
    42. label->setText(QApplication::translate("Dialog", "Test", 0, QApplication::UnicodeUTF8));
    43. } // setupUi
    44. };
    45.  
    46.  
    47. class CNameDlg : public QWidget
    48. {
    49. Q_OBJECT
    50.  
    51. public:
    52. CNameDlg(QWidget *parent = 0);
    53. QLabel *label;
    54. QLineEdit *m_name;
    55.  
    56. void setupUi(QWidget *CNameDlg)
    57. {
    58. if (CNameDlg->objectName().isEmpty())
    59. CNameDlg->setObjectName(QString::fromUtf8("CNameDlg"));
    60. CNameDlg->resize(410, 111);
    61. CNameDlg->setFocusPolicy(Qt::StrongFocus);
    62. label = new QLabel(CNameDlg);
    63. label->setObjectName(QString::fromUtf8("label"));
    64. label->setGeometry(QRect(10, 10, 250, 18));
    65. m_name = new QLineEdit(CNameDlg);
    66. m_name->setObjectName(QString::fromUtf8("m_name"));
    67. m_name->setGeometry(QRect(10, 60, 240, 27));
    68. } // setupUi
    69. };
    70.  
    71. #endif
    To copy to clipboard, switch view to plain text mode 

    and finally focus.pro
    Qt Code:
    1. HEADERS = dialog.h
    2. SOURCES = dialog.cpp \
    3. main.cpp
    4.  
    5. # install
    6. target.path = .
    7. sources.files = $$SOURCES $$HEADERS *.pro
    8. sources.path = .
    9. INSTALLS += target sources
    To copy to clipboard, switch view to plain text mode 

    Note that some of the code was copied from the compile of the .ui files I have.

    If you compile & run this, you'll have a dialog, with a label ("test") and an QLineEdit. The QLineEdit is +not+ the focus.

    Vycke

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    It seems that the problem is caused by "page->setFocus(Qt::OtherFocusReason);" line --- comment it out and it should work. Other solution is make invoke setFocusProxy( m_name ) in CNameDlg.

  11. The following user says thank you to jacek for this useful post:

    vycke (7th January 2008)

  12. #11
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Focus on a QWidget item in a QWidget

    Quote Originally Posted by jacek View Post
    It seems that the problem is caused by "page->setFocus(Qt::OtherFocusReason);" line --- comment it out and it should work. Other solution is make invoke setFocusProxy( m_name ) in CNameDlg.
    Removing the setFocus() call didn't work (4.3.2... might be a bug there). But adding the setFocusProxy() in the contructor of CNameDlg did.

    Thanks so much.

    Vycke

Similar Threads

  1. default on focus different item in messagebox?
    By gfunk in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 05:54
  2. QGraphicsView and item focus
    By Micawber in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2007, 20:36
  3. Replies: 3
    Last Post: 7th November 2006, 08:35
  4. QWidget item in QGraphicsScene
    By IUnknown in forum Qt Programming
    Replies: 4
    Last Post: 4th November 2006, 00:05

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.