#include "mycombobox.h"
#include <QToolButton>
#include <QResizeEvent>
#include <QSize>
{
// Button ---------------------------------------------------------------- //
this->setButtonPosition(this->buttonPositionRight);
button->setCursor(Qt::ArrowCursor);
button->setCheckable(true);
button->setStyleSheet("QToolButton { border: 1px solid #b2c4e5;"
"border-radius: 1px;"
"image: url(../images/downarrow.png);"
"background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
"stop: 0 #e1eafe, stop: 1.0 #bccefa);}"
"QToolButton:checked { border: 1px solid #b0c5f2;"
"background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
"stop: 0 #6e8ef1, stop: 1 #d2deeb);}"
"QToolButton:checked:icon { top: 10px; left: 10px; }"
"QToolButton:!checked:hover { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
"stop: 0 #fdffff, stop: 1 #b9dafb);}");
button->show();
// ----------------------------------------------------------------------- //
}
{
setButtonGeometry(event->size());
}
void MyComboBox::setButtonPosition(buttonPosition pos)
{
if(pos == this->buttonPositionLeft)
buttonPos = this->buttonPositionLeft;
else
buttonPos = this->buttonPositionRight;
setButtonGeometry(this->size());
}
void MyComboBox
::setButtonGeometry(const QSize &lineEditSize
) {
int buttonWidth = 14;
int buttonHeight = lineEditSize.height() - 4;
int buttonX = 2;
int buttonY = 2;
if(buttonPos == this->buttonPositionLeft)
this->setTextMargins(buttonWidth, 0, 0, 0);
else
{
// Sets the button to be on the right side and sets margins of the QLineEdit
buttonX = lineEditSize.width() - (buttonWidth + buttonY);
this->setTextMargins(0, 0, buttonWidth, 0);
}
button->setGeometry(buttonX, buttonY, buttonWidth, buttonHeight);
}
#include "mycombobox.h"
#include <QToolButton>
#include <QResizeEvent>
#include <QSize>
MyComboBox::MyComboBox(QWidget *parent) : QLineEdit(parent)
{
// Button ---------------------------------------------------------------- //
button = new QToolButton(this);
this->setButtonPosition(this->buttonPositionRight);
button->setCursor(Qt::ArrowCursor);
button->setCheckable(true);
button->setStyleSheet("QToolButton { border: 1px solid #b2c4e5;"
"border-radius: 1px;"
"image: url(../images/downarrow.png);"
"background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
"stop: 0 #e1eafe, stop: 1.0 #bccefa);}"
"QToolButton:checked { border: 1px solid #b0c5f2;"
"background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
"stop: 0 #6e8ef1, stop: 1 #d2deeb);}"
"QToolButton:checked:icon { top: 10px; left: 10px; }"
"QToolButton:!checked:hover { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,"
"stop: 0 #fdffff, stop: 1 #b9dafb);}");
button->show();
// ----------------------------------------------------------------------- //
}
void MyComboBox::resizeEvent(QResizeEvent *event)
{
setButtonGeometry(event->size());
}
void MyComboBox::setButtonPosition(buttonPosition pos)
{
if(pos == this->buttonPositionLeft)
buttonPos = this->buttonPositionLeft;
else
buttonPos = this->buttonPositionRight;
setButtonGeometry(this->size());
}
void MyComboBox::setButtonGeometry(const QSize &lineEditSize)
{
int buttonWidth = 14;
int buttonHeight = lineEditSize.height() - 4;
int buttonX = 2;
int buttonY = 2;
if(buttonPos == this->buttonPositionLeft)
this->setTextMargins(buttonWidth, 0, 0, 0);
else
{
// Sets the button to be on the right side and sets margins of the QLineEdit
buttonX = lineEditSize.width() - (buttonWidth + buttonY);
this->setTextMargins(0, 0, buttonWidth, 0);
}
button->setGeometry(buttonX, buttonY, buttonWidth, buttonHeight);
}
To copy to clipboard, switch view to plain text mode
#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QLineEdit>
{
Q_OBJECT
Q_ENUMS(buttonPosition)
public:
void resizeEvent
(QResizeEvent *event
);
// ...for button position... enum buttonPosition {buttonPositionLeft = true, buttonPositionRight = false};
void setButtonPosition(buttonPosition);
protected:
// Button --------------------------------------------------------------------------------- //
buttonPosition buttonPos; // Button position (true -> Left, false -> Right)
void setButtonGeometry
(const QSize &);
// Sets button position and size // ---------------------------------------------------------------------------------------- //
};
#endif // MYCOMBOBOX_H
#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QLineEdit>
class QToolButton;
class QLineEdit;
class QSize;
class MyComboBox : public QLineEdit
{
Q_OBJECT
Q_ENUMS(buttonPosition)
public:
MyComboBox(QWidget *parent = 0);
void resizeEvent(QResizeEvent *event); // ...for button position...
enum buttonPosition {buttonPositionLeft = true, buttonPositionRight = false};
void setButtonPosition(buttonPosition);
protected:
// Button --------------------------------------------------------------------------------- //
QToolButton *button; // Button to show drop-down list
buttonPosition buttonPos; // Button position (true -> Left, false -> Right)
void setButtonGeometry(const QSize &); // Sets button position and size
// ---------------------------------------------------------------------------------------- //
};
#endif // MYCOMBOBOX_H
To copy to clipboard, switch view to plain text mode
Bookmarks