Results 1 to 9 of 9

Thread: Imitating a ComboBox

  1. #1
    Join Date
    Aug 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Talking Imitating a ComboBox

    Hi all. My first post here
    I'm trying to "imitate" a combo box. Original combo doesn't cut it and I can't achieve what I need by sublassing it so I decided to make one. I used a QLineEdit and putted a QToolButton in it. It works fine but there’s one thing bothering me. Button height is 4 pixels smaller than that of line edit. Thus if a user puts his mouse over a frame of line edit, that can be seen below and above button, the mouse pointer will change to text cursor. I figure I could simply put some sort of rect behind button that will have the same height like line edit, be transparent and will do nothing except stop mouse pointer from changing. I tried, and tried but nothing that came across my mind worked. Maybe because I'm a beginner in QT

  2. #2
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Imitating a ComboBox

    You did not mention why you don't use QComboBox. You write "Original combo doesn't cut it". Cut what? Cut the text if it is too long?
    Maybe QComboBox can do what you want, but you just didn't find it.

  3. #3
    Join Date
    Aug 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Imitating a ComboBox

    Quote Originally Posted by Boron View Post
    You did not mention why you don't use QComboBox. You write "Original combo doesn't cut it". Cut what? Cut the text if it is too long?
    Maybe QComboBox can do what you want, but you just didn't find it.
    By "doesn't cut it" I meant I didn't find a way to achieve what I had in mind mostly because I’m a beginner I need a user to be able to enter text while list of possible choices is displayed as a drop-down list. With a combo when you show the drop down list you can’t enter anything until list is closed. I tried using QCompleter. But couldn’t get it to show the current completion as selected. It just puts a doted frame around current completion.

  4. #4
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Imitating a ComboBox

    You can have a look in the QtCreator sourcecode for the special lineedit bottom left of the mainwindow. It does autocomplete while you are typing.

    Also see http://qt.nokia.com/doc/qq/qq07-cust...ompletion.html but be warned, it is Qt 3.
    It's nice to be important but it's more important to be nice.

  5. #5
    Join Date
    Aug 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Imitating a ComboBox

    Quote Originally Posted by axeljaeger View Post
    You can have a look in the QtCreator sourcecode for the special lineedit bottom left of the mainwindow. It does autocomplete while you are typing.

    Also see http://qt.nokia.com/doc/qq/qq07-cust...ompletion.html but be warned, it is Qt 3.

    Thank you. Code in that document has similar functionality that I’m trying to achieve, and is pretty close to what I had in mind on how to solve this. The button is there to let user choose without having to type anything. Any idea on how to solve my mouse pointer problem?

  6. #6
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Imitating a ComboBox

    Do you put the button INSIDE the text field? Why not put it next to the lineedit using a layout?
    It's nice to be important but it's more important to be nice.

  7. #7
    Join Date
    Aug 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Imitating a ComboBox

    Quote Originally Posted by axeljaeger View Post
    Do you put the button INSIDE the text field? Why not put it next to the lineedit using a layout?
    Yes, it is inside lineEdit. It looks more like true combo that way. Here's how I did it so far.

    .cpp
    Qt Code:
    1. #include "mycombobox.h"
    2. #include <QToolButton>
    3. #include <QResizeEvent>
    4. #include <QSize>
    5.  
    6. MyComboBox::MyComboBox(QWidget *parent) : QLineEdit(parent)
    7. {
    8. // Button ---------------------------------------------------------------- //
    9. button = new QToolButton(this);
    10. this->setButtonPosition(this->buttonPositionRight);
    11. button->setCursor(Qt::ArrowCursor);
    12. button->setCheckable(true);
    13. button->setStyleSheet("QToolButton { border: 1px solid #b2c4e5;"
    14. "border-radius: 1px;"
    15. "image: url(../images/downarrow.png);"
    16. "background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
    17. "stop: 0 #e1eafe, stop: 1.0 #bccefa);}"
    18. "QToolButton:checked { border: 1px solid #b0c5f2;"
    19. "background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
    20. "stop: 0 #6e8ef1, stop: 1 #d2deeb);}"
    21. "QToolButton:checked:icon { top: 10px; left: 10px; }"
    22. "QToolButton:!checked:hover { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
    23. "stop: 0 #fdffff, stop: 1 #b9dafb);}");
    24. button->show();
    25. // ----------------------------------------------------------------------- //
    26. }
    27.  
    28. void MyComboBox::resizeEvent(QResizeEvent *event)
    29. {
    30. setButtonGeometry(event->size());
    31. }
    32.  
    33. void MyComboBox::setButtonPosition(buttonPosition pos)
    34. {
    35. if(pos == this->buttonPositionLeft)
    36. buttonPos = this->buttonPositionLeft;
    37. else
    38. buttonPos = this->buttonPositionRight;
    39.  
    40. setButtonGeometry(this->size());
    41. }
    42.  
    43. void MyComboBox::setButtonGeometry(const QSize &lineEditSize)
    44. {
    45. int buttonWidth = 14;
    46. int buttonHeight = lineEditSize.height() - 4;
    47. int buttonX = 2;
    48. int buttonY = 2;
    49.  
    50. if(buttonPos == this->buttonPositionLeft)
    51. this->setTextMargins(buttonWidth, 0, 0, 0);
    52. else
    53. {
    54. // Sets the button to be on the right side and sets margins of the QLineEdit
    55. buttonX = lineEditSize.width() - (buttonWidth + buttonY);
    56. this->setTextMargins(0, 0, buttonWidth, 0);
    57. }
    58.  
    59. button->setGeometry(buttonX, buttonY, buttonWidth, buttonHeight);
    60. }
    To copy to clipboard, switch view to plain text mode 

    .h
    Qt Code:
    1. #ifndef MYCOMBOBOX_H
    2. #define MYCOMBOBOX_H
    3.  
    4. #include <QLineEdit>
    5.  
    6. class QLineEdit;
    7. class QSize;
    8.  
    9. class MyComboBox : public QLineEdit
    10. {
    11. Q_OBJECT
    12. Q_ENUMS(buttonPosition)
    13.  
    14. public:
    15. MyComboBox(QWidget *parent = 0);
    16. void resizeEvent(QResizeEvent *event); // ...for button position...
    17. enum buttonPosition {buttonPositionLeft = true, buttonPositionRight = false};
    18. void setButtonPosition(buttonPosition);
    19.  
    20.  
    21. protected:
    22. // Button --------------------------------------------------------------------------------- //
    23. QToolButton *button; // Button to show drop-down list
    24. buttonPosition buttonPos; // Button position (true -> Left, false -> Right)
    25. void setButtonGeometry(const QSize &); // Sets button position and size
    26. // ---------------------------------------------------------------------------------------- //
    27. };
    28.  
    29. #endif // MYCOMBOBOX_H
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Imitating a ComboBox

    No no, don't do that.

    Only because "it looks more like a real combobox" on some operating system, the approach is doomed to fail on other operating systems/skins.

    You will also be able to have text UNDER your button. Thats not good also. Make both lineedit and button next to each other, disable the frame of the lineedit and maybe make a frame around all. That is basically how the real combo is implemented.

    See also http://labs.trolltech.com/blogs/2007...-clear-button/
    It's nice to be important but it's more important to be nice.

  9. #9
    Join Date
    Aug 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Imitating a ComboBox

    Quote Originally Posted by axeljaeger View Post
    No no, don't do that.

    Only because "it looks more like a real combobox" on some operating system, the approach is doomed to fail on other operating systems/skins.

    You will also be able to have text UNDER your button. Thats not good also. Make both lineedit and button next to each other, disable the frame of the lineedit and maybe make a frame around all. That is basically how the real combo is implemented.

    See also http://labs.trolltech.com/blogs/2007...-clear-button/
    For text under the button I used setTextMargins to avoid it. Also I doubt that my application will ever be used on other operating system than widows, but even difference between the way XP and Vista look could pose a problem, so your argument is completely in place. I will try the approach you mentioned and if I get stuck I’ll be back with more questions

Similar Threads

  1. Combobox with checkable items
    By qtneuling in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2008, 14:42
  2. Replies: 2
    Last Post: 18th March 2008, 15:38
  3. ComboBox of bmp:s
    By SailinShoes in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2008, 15:22
  4. [Qt 4.3.1]A problem with combobox style
    By Tamara in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2007, 10:49
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

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
  •  
Qt is a trademark of The Qt Company.