I would like to know how many pixels need this items in QComboBox:
red - checkbox
green - distance between end of checkbox and beggining of the text
blue - distance between end of border and beggining of the checkbox
In doc of QStyle I find two methods:
subElementRect()
pixelMetric()
I think I have to use them, but I don't know, which args I need to use.
It depends on the style.
QCommonStyle for example draws the ComboBox's label like this:
if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
QRect editRect = proxy()->subControlRect(CC_ComboBox, cb, SC_ComboBoxEditField, widget);
p->save();
p->setClipRect(editRect);
if (!cb->currentIcon.isNull()) {
QIcon::Mode mode = cb->state & State_Enabled ? QIcon::Normal
: QIcon::Disabled;
QPixmap pixmap = cb->currentIcon.pixmap(qt_getWindow(widget), cb->iconSize, mode);
QRect iconRect(editRect);
iconRect.setWidth(cb->iconSize.width() + 4);
iconRect = alignedRect(cb->direction,
Qt::AlignLeft | Qt::AlignVCenter,
iconRect.size(), editRect);
if (cb->editable)
p->fillRect(iconRect, opt->palette.brush(QPalette::Base));
proxy()->drawItemPixmap(p, iconRect, Qt::AlignCenter, pixmap);
if (cb->direction == Qt::RightToLeft)
editRect.translate(-4 - cb->iconSize.width(), 0);
else
editRect.translate(cb->iconSize.width() + 4, 0);
}
if (!cb->currentText.isEmpty() && !cb->editable) {
proxy()->drawItemText(p, editRect.adjusted(1, 0, -1, 0),
visualAlignment(cb->direction, Qt::AlignLeft | Qt::AlignVCenter),
cb->palette, cb->state & State_Enabled, cb->currentText);
}
p->restore();
}
This means, that the actual size of the icon's rectangle can be determined by subclassing QComboBox (to access its protected initStyleOption method) and creating a new public getIconRect method in the following way:
QRect ComboBox::getIconRect()
{
QStyleOptionComboBox opt;
initStyleOption(&opt);
QRect rect(style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField));
rect.setWidth(opt.iconSize.width() + 4);
return rect;
}
Then in MainWindow for example create a ComboBox and call its getIconSize method like this:
auto *cmbBox = new ComboBox(this);
qDebug() << cmbBox->getIconRect();
For me, on Windows 10, this gives:
QRect(3,3 20x14)
There could be other ways, but if you insist on using a method similar to QStyle::subControlRect, those are the correct arguments;
Related
I've got four QLineEdit placed inside of a QLineEdits, where I want the first the parent to look as if it is in focus when any of the containing ones is selected. Note: I don't want the focus to actually change, just the "focus frame" (the thin blue border) to appear on the parent LineEdit.
I've tried to draw a rect, but while it works on Windows I'm running into issues of the drawn rectangle not looking like a proper rectangle on ex. Linux, where it is supposed to be rounded. Is there a way to fix this OR, if possible, just make it draw itself as focused despite focus not being on it?
Here's my attempt at drawing a custom rect, but haven't been able to make it successfully mirror the OS style properly.
if (childHasFocus) {
QPainter painter(this);
QLineEdit textBox;
QColor color = textBox.palette().color(QPalette::Highlight);
painter.setPen(color);
QRect rect;
rect.setTopLeft(QPoint(0,0));
rect.setWidth(this->width() - 1);
rect.setHeight(this->height() - 1);
painter.drawRect(rect);
}
EDIT: Added an image of the desired look. Note that I'm trying to get it to look like other LineEdits focusframe independent of OS, so hardcoding a blue rectangle won't work due to ex. Linux having a rounded focusframe.
Desired look:
Here's how to do it. Its a very basic class that draws the focus frame if any of the childs have focus. On focus change, we do an update (which can probably be optimized a bit to avoid unnecessary repaints).
Screenshot:
class IPEdit : public QWidget
{
public:
IPEdit(QWidget *parent = nullptr)
: QWidget(parent)
{
delete layout();
auto l = new QHBoxLayout(this);
setFocusProxy(&a);
setAttribute(Qt::WA_Hover);
for (auto *w : {&a, &b, &c, &d}) {
l->addWidget(w);
w->installEventFilter(this);
}
}
bool eventFilter(QObject *o, QEvent *e) override
{
if (e->type() == QEvent::FocusIn || e->type() == QEvent::FocusOut) {
update();
}
return QWidget::eventFilter(o, e);
}
void paintEvent(QPaintEvent *e) override
{
QStyleOptionFrame opt;
opt.initFrom(this);
opt.frameShape = QFrame::StyledPanel;
opt.state |= QStyle::State_Sunken;
// clear mouseOver and focus state
// update from relevant widgets
opt.state &= ~(QStyle::State_HasFocus | QStyle::State_MouseOver);
const auto widgets = {&a, &b, &c, &d};
for (const QWidget *w : widgets) {
if (w->hasFocus()) {
opt.state |= QStyle::State_HasFocus;
}
}
opt.rect = contentsRect();
QPainter paint(this);
paint.setClipRegion(e->region());
paint.setRenderHints(QPainter::Antialiasing);
style()->drawControl(QStyle::CE_ShapedFrame, &opt, &paint, this);
}
private:
QLineEdit a;
QLineEdit b;
QLineEdit c;
QLineEdit d;
};
QlineEdit class is also a qwidget, use the setFocus method
https://doc.qt.io/qt-6/qwidget.html#setFocus
I want to paint a circle with user provided color and keep a line edit adjustment on it on horizontal alignment.
Used painter function call on slot, but its not working
#include <QPainter>
#include "cascadeColorHighlightWidget.h"
CascadeColorHighlightWidget::CascadeColorHighlightWidget(QWidget *parent) : QWidget(parent)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::Widget);
setAttribute( Qt::WA_DeleteOnClose, true );
setFixedSize(187,164);
setContentsMargins(0,0,0,0);
}
void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
painter.setPen(QPen(QColor(176, 176, 176),1));
painter.setBrush(QColor(255,255,255));
painter.drawRect(contRect);
painter.setPen(QPen(QColor(51,51,51),1));
QFont font( "Calibri" );
font.setPixelSize(14);
painter.setFont( font );
painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight");
}
void CascadeColorHighlightWidget::focusOutEvent(QFocusEvent *event)
{
Q_UNUSED(event);
close();
}
void CascadeColorHighlightWidget::setColors(QColor color)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
int rectYPos = contRect.y() + 55;
painter.setPen(Qt::NoPen);
QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16);
painter.setPen(Qt::NoPen);
painter.setBrush(color);
painter.drawEllipse(ellipseRect);
/*After this ellipse I need to draw a line edit where user can edit anytime*/
}
But by calling setcolot its not drawing the ellipse on the widget. Only the items in paintEvent worked.
Is it possible to do with painter or I need to keep widgetItems and insert in this wideget. please give some suggestions
All painting work should happen in paintEvent. You have to keep state, and paint items accordingly. Have methods that take QPainter as an argument and call them from within paintEvent method, passing to them the QPainter object you created there.
Example:
In your widget header have:
private:
void setColors(QColor c) { color = c; }
void drawEllipse(QPainter & painter);
QColor color;
bool draw_ellipse;
As you can see, the setColors method only sets a color and you keep that color in a private instance variable color.
A new method hosts the painting job (previously in setColors):
void CascadeColorHighlightWidget::drawEllipse(QPainter &painter)
{
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
int rectYPos = contRect.y() + 55;
painter.setPen(Qt::NoPen);
QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16);
painter.setPen(Qt::NoPen);
painter.setBrush(color);
painter.drawEllipse(ellipseRect);
/*After this ellipse I need to draw a line edit where user can edit anytime*/
}
The variable color in this line
painter.setBrush(color);
is the one you set using the setColors method.
The paintEvent method should be like:
void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QRectF contRect = contentsRect().adjusted(1, 1, -1, -1);
painter.setPen(QPen(QColor(176, 176, 176),1));
painter.setBrush(QColor(255,255,255));
painter.drawRect(contRect);
painter.setPen(QPen(QColor(51,51,51),1));
QFont font( "Calibri" );
font.setPixelSize(14);
painter.setFont( font );
painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight");
if(draw_ellipse)
{
drawEllipse(painter);
}
}
At the end of it, you test draw_ellipse (don't forget to initialize it to false in the constructor) and call the drawEllipse method if it's true.
Let's draw the ellipse, for example using QWidget's mousePressEvent:
void CascadeColorHighlightWidget::mousePressEvent(QMouseEvent *event)
{
setColors(QColor(Qt::red));
draw_ellipse = true;
update();
}
Here, you set a color, first, then set draw_ellipse to true, then (and it matters a lot) you call the update slot of QWidget:
[...] it schedules a paint event for processing when Qt returns to the
main event loop.
So the paintEvent method will be called, and your paintings updated accordingly to your class's state (color and draw_ellipse variables).
I have a QTableView with my own implemented QAbstractItemModel, in which I can drag and drop multiple items inside. My problem is that when dragging the items and while trying to drop them in a destination cell, it is not so obvious for the user what the result is going to be. For example, I have the following,but I would prefer sth like the default widows displaying, which makes all 3 items like one item:
my QT Table
vs
windows dragging n dropping folders
After eyllanesc's suggestion for QPixmap, I found the correct solution to my problem, so that I can keep the mime data coming from my model. I have re-implemented startDrag(Qt::DropActions supportedActions) in my QTreeView class, so that when multiple objects are moved, one icon will be displayed along with the number of items moved. Now looks like this:
void MyTreeView::startDrag(Qt::DropActions supportedActions)
{
QModelIndexList indexes = selectedIndexes();
if (indexes.size() == 1)
return QAbstractItemView::startDrag(supportedActions);
if (indexes.count() > 0)
{
QMimeData *data = model()->mimeData(indexes);
if (!data)
return;
QRect rect;
rect.adjust(horizontalOffset(), verticalOffset(), 0, 0);
QDrag *drag = new QDrag(this);
ActionTreeItem* pItem = static_cast<ActionTreeItem*>(indexes[0].internalPointer());
if (pItem != NULL)
{
QPixmap pixmap = myIcon.pixmap(myIcon.actualSize(QSize(32, 32)));
QPainter *paint = new QPainter(&pixmap);
paint->setPen(Qt::black);
paint->setBrush(QBrush(Qt::white));
QRect numberRect(18, 18, 13, 13);
paint->drawRect(numberRect);
paint->drawText(numberRect, Qt::AlignHCenter | Qt::AlignVCenter, QString("%1").arg(indexes.count()));
drag->setPixmap(pixmap);
}
drag->setMimeData(data);
Qt::DropAction defaultDropAction = Qt::MoveAction;
drag->exec(supportedActions, defaultDropAction);
}
}
Taking this tutorial as a reference, the mousePressEvent method is overwritten, and a new QPixmap is placed in QDrag:
void mousePressEvent(QMouseEvent *event){
if (event->button() == Qt::LeftButton){
QDrag *drag = new QDrag(this);
drag->setMimeData(new QMimeData());
drag->setPixmap(QPixmap("image.png"));
drag->exec();
}
QTableView::mousePressEvent(event);
}
Output:
When I set the placeholder text with QLineEdit::setPlaceholderText(), it appears gray.
Is there any way to change the color to something else, for example red?
You'll have to subclass QLineEdit and paint your own placeholder in the paintEvent().
class CustomColorPlaceholderLineEdit : public QLineEdit
{
public:
CustomColorPlaceholderLineEdit(QWidget * parent = 0) : QLineEdit(parent) { color = QColor(0,0,0,128); }
void setCustomPlaceholderText(const QString &text) { this->mText = text; }
const QString &customPlaceholderText() const { return mText; }
void setCustomPlaceholderColor(const QColor &color) { this->color = color; }
const QColor &customPlaceholderColor() const { return color; }
void paintEvent(QPaintEvent *event) {
QLineEdit::paintEvent(event);
if (!hasFocus() && text().isEmpty() && !mText.isEmpty()) {
// QLineEdit's own placeholder clashes with ours.
Q_ASSERT(placeholderText().isEmpty());
QPainter p(this);
p.setPen(color);
QFontMetrics fm = fontMetrics();
int minLB = qMax(0, -fm.minLeftBearing());
QRect lineRect = this->rect();
QRect ph = lineRect.adjusted(minLB + 3, 0, 0, 0);
QString elidedText = fm.elidedText(mText, Qt::ElideRight, ph.width());
p.drawText(ph, Qt::AlignVCenter, elidedText);
}
}
private:
QString mText;
QColor color;
};
There is another a bit hacky but simple and reliable way.
connect(lineEdit, &QLineEdit::textChanged, this, &YourClass::updateLineEditStyleSheet);
void YourLineEdit::updateLineEditStyleSheet()
{
if (lineEdit->text().isEmpty()) {
lineEdit->setStyleSheet("#lineEdit { color: lightGray;"); // Set your color but remember that Qt will reduce alpha
} else {
lineEdit->setStyleSheet("#lineEdit { color: black;"); // usual color
}
}
also you can use this way to derived from QLineEdit class
If you want to use QSS instead of QPalette, try the following:
setStyleSheet("QLineEdit{"
" color: red;" //TEXT COLOR
"}"
"QLineEdit[text=\"\"]{"
" color: gray;" //TEXTHOLDER COLOR
"}");
connect(ui->lineEdit, &QLineEdit::textChanged, [=]{ style()->polish(ui->lineEdit); });
You can change the color, but bare in mind there is an alpha factor set in the placeholder from the source code (as mentioned in another comment) that cannot be removed. Therefore you will always see the placeholder darker (no white possible with this option).
You can't, at least with the current QLineEdit code.
As you can see from the source code, the placeholder text is simply taking the foreground brush of the palette and making it partially transparent, see QLineEdit::paintEvent:
if (d->shouldShowPlaceholderText()) {
if (!d->placeholderText.isEmpty()) {
QColor col = pal.text().color();
col.setAlpha(128);
QPen oldpen = p.pen();
p.setPen(col);
QRect ph = lineRect.adjusted(minLB, 0, 0, 0);
QString elidedText = fm.elidedText(d->placeholderText, Qt::ElideRight, ph.width());
p.drawText(ph, va, elidedText);
p.setPen(oldpen);
}
}
You can work with upstream into a more general solution, though. In particular I one would expect that color to be added to the palette, or in general provided by the current QStyle (for instance as a style hint).
If you want to change placeholder text color for a QLineEdit you have to customize the component's QPalette object.
QPalette p = lineEdit->palette();
p.setColor(QPalette::Mid, Qt::red); // assuming Mid is the color you want to change.
lineEdit->setPalette(p);
I don't recall exactly which QPalette::ColorRole is appropriate for changing QLineEdit's placeholder text color though.
#Meefte solution is quite good given the situation that Qt gives placeholder the same color as for the text, except it adds 50% opacity. So, there is little choice to set placeholder color to be different than the text. However, even this solution could be improved by making sure that you would not need to set some other variable than the default one Qt provides you.
The need to use default placeholderText() might arise from the situation when you have lots of QLineEdit controls which are already promoted to some control overriding QLineEdit behavior, and placeholderText() is already set through code or through Qt Creator, i.e. it would be a bit painful to introduce another dynamic property. However, if you did not promote to some child control, then it would be a necessity to do so in order to use such solution.
class CustomColorPlaceholderLineEdit : public QLineEdit
{
public:
CustomColorPlaceholderLineEdit(QWidget * parent = 0) : QLineEdit(parent) { color = QColor(0,0,0,128); }
const QString &customPlaceholderText() const { return mText; }
void setCustomPlaceholderColor(const QColor &color) { this->color = color; }
const QColor &customPlaceholderColor() const { return color; }
void paintEvent(QPaintEvent *event)
{
if(color.isValid() && text().isEmpty() && (!placeholderText().isEmpty() || !mText.isEmpty()))
{
if(!placeholderText().isEmpty())
{
// In this way, placeholderText() is taken into local variable 'mText' care. Whenever placeholderText() will change, there it will be taken care of.
mText = placeholderText();
// This will ensure Qt will not draw placeholder for us.
setPlaceholderText("");
}
// By this, we make sure Qt will paint QLineEdit default parts properly.
QLineEdit::paintEvent(e);
// And now #Meefte code is reused here.
QPainter p(this);
p.setPen(color);
QFontMetrics fm = fontMetrics();
int minLB = qMax(0, -fm.minLeftBearing());
QRect lineRect = this->rect();
QRect ph = lineRect.adjusted(minLB + 3, 0, 0, 0);
QString elidedText = fm.elidedText(mText, Qt::ElideRight, ph.width());
p.drawText(ph, Qt::AlignVCenter, elidedText);
return; // No need to paint again.
}
// Default Qt's painting behavior for QLineEdit.
QLineEdit::paintEvent(e);
}
private:
QString mText;
QColor color;
};
QT still has this problem)
I solved it like this:
bool CustomLineEdit::event(QEvent *event)
{
bool eventResult = QLineEdit::event(event);
if (event->type() == QEvent::StyleChange) {
QPalette pal = palette();
pal.setColor(QPalette::PlaceholderText, Qt::red);
setPalette(pal);
}
return eventResult;
}
I am trying to elide text in a QLabel (subclassed into Label). The problem is that the custom paintEvent() doesn't respect the variables set by the stylesheet (especially padding/margin). How do I fix this?
As you can see I have one solution which does mostly the right thing, but the margins don't function properly. The other solution is far from working but along the right path. I know that I need to do something with QStyle to get the size of the area, but I dont understand it. The documentation is really confusing.
void Label::paintEvent(QPaintEvent *event){
if (this->elide == Qt::ElideNone){
QLabel::paintEvent(event);
return;
}
QPainter p(this);
QFontMetrics fm(font());
if (fm.width(text()) > contentsRect().width()) {
if (1){ //this kind of works...
QRect rect = this->contentsRect();
rect.adjust(this->margin(), this->margin(), -this->margin(), -this->margin());
QString elided_txt = this->fontMetrics().elidedText(text(), this->elide, rect.width(), Qt::TextShowMnemonic); //This is the key line.
p.drawText(rect(), elided_txt, QTextOption(Qt::AlignVCenter | Qt::AlignLeft));
} else { //the correct solution should look something like this:
QStyle *style = this->style();
QRect rect = style->itemTextRect(fm, this->rect(), Qt::AlignVCenter | Qt::AlignHCenter, true, this->text());
QString elided_txt = this->fontMetrics().elidedText(text(), this->elide, rect.width(), Qt::TextShowMnemonic); //This is the key line.
style->drawItemText(&p, this->rect(), Qt::AlignVCenter | Qt::AlignLeft, this->palette(), true, elided_txt);
}
} else {
QLabel::paintEvent(event);
}
}