Qt 5.3 QPlainTextEdit Change the QTextCursor color - c++

I would like to change the cursor color under the QPlainTextEdit widget. I was able to set it's width to 6, but I want to change the color or it. Is it possible ?
QFontMetrics fm(font());
setCursorWidth( fm.averageCharWidth() );
//setCursorColor is what I need.
Thanks.
Edit:
Including the images to exemplify...
From this:
To this:
Thanks.
Edit2: Final Look

You can use QTextCharFormat to set the color of the text in your QPlainTextEdit. Use the QTextCharFormat::setForeground to set the color.
Then use a stylesheet to change the color of the cursor by using the color property.
QPlainTextEdit *p_textEdit = new QPlainTextEdit;
p_textEdit->setStyleSheet("QPlainTextEdit{color: #ffff00; background-color: #303030;"
" selection-background-color: #606060; selection-color: #ffffff;}");
QTextCharFormat fmt;
fmt.setForeground(QBrush(QColor(255,255,255)));
p_textEdit->mergeCurrentCharFormat(fmt);

Edit
You can change caret color with next stylesheet:
ui->plainTextEdit->setStyleSheet(
"QPlainTextEdit"
"{"
"color: yellow;"
"}"
);
But there is another problem, all text becomes with this color too. How to set new color to text but left old color for caret? I found this solution, maybe not the best: use html code:
ui->plainTextEdit->appendHtml("<font color = \"red\"> Sample Text</font>");
Result (as you want original color for caret and for text):
Now text has needed color but caret has special color. It is solution, but it is a little dirty for me, if someone will find better way to change color of text without changing caret color, please tell this.
You can change only main cursor under widget:
QPixmap pix("pathToPixmap");
QCursor cur(pix);
ui->plainTextEdit->viewport()->setCursor(cur);
Qt has next embedded cursors: http://qt-project.org/doc/qt-5/qt.html#CursorShape-enum
Qt has not any cursor with specific color, so you should use you own pixmap. You can use image with simple arrow but with another color (if it has alpha-channel then it is better)
There are many different cursors in the web.

Related

Qt/C++ - Set background of disabled button?

I have all my buttons disabled in a grid, but for some, I'd like to change the background color. I'm trying:
_fieldButtons[0][0]->setStyleSheet("color: blue; background-color: blue;");
Where
QVector<QVector<QPushButton*> > _fieldButtons;
However, these buttons are all disabled, and only the text color gets changed:
How can I change the background, but not the text? Thank you!
UPDATE
I figured it's not working because the buttons are flat. How can I change flat button colors?
Two options:
Add border: none; to your stylesheet.
Use setAutoFillBackground(true) in conjunction with QPalette::Button
myButton->setFlat(true);
myButton->setAutoFillBackground(true);
QPalette pal = myButton->palette();
pal.setColor(QPalette::Button, QColor(Qt::blue));
myButton->setPalette(pal);
http://doc.qt.io/qt-5/qpushbutton.html#flat-prop
Try this:
myButton->setPalette(QColor("#124e6b"));
simply change the QColor to suit your use.
Or in Qt Creator you can right-click on the widget and select Change Style Sheet, as shown here:
Here you have two "Pseudo-States" in your control. For list of "Pseudo-States" refer below link.
http://doc.qt.io/qt-5/stylesheet-reference.html#list-of-pseudo-states
The first "Pseudo-State" is -- flat
The second "Pseudo-State" is -- disabled
Here you have to club both the states to set the style using "setStyleSheet".
_fieldButtons[0][0]->setStyleSheet(":flat:disabled {background-color: blue; border: none;}");
look for ":hover:enabled" (two different "Pseudo-States" how documentation handled) in the below link to get better idea.
http://doc.qt.io/qt-4.8/stylesheet-syntax.html
To understand, why you we have to give border:none for QPushButton, please look for below information in the first hyperlink in this answer.
"Warning: If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color."

Set background color for QTabBar beyond tabs?

Simple as the question title, I have a QTabWidget and I wanted to set the background color for the QTabBar area, and this is how I do it:
ViewTabs->tabBar()->setStyleSheet("background-color: rgb(85,85,85)");
I expected this to set the back ground for the whole bar, but instead it sets the background on tabs only as in the attached picture.
How do I make the background color take effect even beyond the tabs? I'm using QT 5.7 with C++.
You can try to change the background color manually in the UI interface by clicking on "palette" in the proprieties.
It's easier to handle colors this way.

Qt changing the background color of QWidget using palette doesn't work

I want to change the background color in a custom subclass of QWidget.
Here is the code:
WorldView::WorldView(QWidget *parent) : QWidget(parent)
{
QPalette p(palette());
p.setColor(QPalette::Background, Qt::black);
setAutoFillBackground(true);
setPalette(p);
}
But it doesn't work as expected. The background color remains unchanged.
I don't know why.
As you can read in the documentation, QPalette::Background is obsolete. Use QPalette::Window instead. Note that some widgets use some other role for the background. See the QPalette::ColorRole documentation
Also:
Warning: Some styles do not use the palette for all drawing, for
instance, if they make use of native theme engines. This is the case
for both the Windows XP, Windows Vista, and the OS X styles.
In this case I suggest to use style sheets. See Qt Style Sheets Reference
However, if WorldView is a custom widget, with your custom paintEvent, it's up to you to draw the background
Use QPalette::Base instead of QPalette::Background
QPalette p(palette());
p.setColor(QPalette::Base, Qt::lightGray);
setPalette(p);
For some reason custom widget uses QPalette::Base for filling backgound
setStyleSheet('background-color:black;')
If the palette color doesnt work then it can have four reasons you should check
you have set the colors from style sheets which i don't recommend at all or maybe you have set the parents stylesheet
your color role in the palette doesnt match
make sure the palette is set for widget and i dont recommend to set palette for every widget unless you really wanted to, you should set the palette for your application
or maybe your window manager is forcing colors and palettes dont work at all
Thats all i got in my mind, if you know more, please edit this answere and add it to items

QSciScintilla Background Color

I'm trying to change the color of the entire QSciScintilla editor widget. I've tried using the function
QSciScintilla::setPaper(const QColor &c)
but that seems to change only the color behind the text (see screenshot). How can I change the background color of the ENTIRE box?
Thanks in advance.
Calling setPaper on the QsciScintilla widget will have no effect if a lexer has been set.
Try using the setDefaultPaper function of the current lexer.

Qt: background for QtAbstractItemView (QTableView) while editing an entry

I have the following question.
My QTableView has background color set to black and color (of contents) to white. So, white text appears on a black background - everything seems to be correct. However, when editing (typing in editing mode) content color is changed to black and it becomes completely invisible due to black background, but editing works fine. After confirming - color reverts back to white. How to set color of currently-being-edited text to white (preferably via stylesheets) or stop such change in this case?
You have to use the :edit-focus and/or :focus states in your stylesheet.
QTableView:edit-focus {
// style here
}
For a list of all available states have a look here
Setting palette worked finally.
QPalette palette;
palette.setColor(QPalette::Text, Qt::white);
qApp->setPalette(palette);