Qt: change color part of text - qt

This is my code:
QTextCursor cursor = ui->editor->textCursor(); // editor is QTextEdit
cursor.select(QTextCursor::WordUnderCursor);
QString c = cursor.selectedText();
if (c == keywords[i])
{
cursor.removeSelectedText();
cursor.insertHtml("<font color=\"DeepPink\">" + keywords[i] + "</font>");
}
So, if the keyword is "new", this word is colored pink. The problem is that everything that is inserted after "new" is also colored red, and not the standard black.
Anybody? :)
UPDATE:
Stupid me. Just added ui->editor->setTextColor("#000000");

ui->editor->setTextColor("#000000");

Related

Qt How to change selected text char by char

I'm creating a simple Text Editor in Qt.
I'm able to edit selected text and make it, for example, bold or underline or both. The problem is when the selected text is partially bold, normal or other.
So the only way to make it good is to take the selected text and edit it char by char (if it is already cursive and i want it bold too, the char must be both).
This is part of my code in which i can change selected text into bold:
QFont font;
QTextCursor cursor = ui->textEdit->textCursor();
QTextCharFormat format;
if(cursor.hasSelection()){
font = cursor.charFormat().font();
if(!cursor.charFormat().font().bold()){
font.setBold(true);
format.setFont(font);
cursor.setCharFormat(format);
}
else{
font.setBold(false);
format.setFont(font);
cursor.setCharFormat(format);
}
ui->textEdit->setTextCursor(cursor);
The cursive function is identical.
This will not work with text already edit.
Example:
randomtext
Now i want to select part of the text such as: "ndomte" and make it all bold. My result is:
randomtext
What i want is:
randomtext
How can i do it?
Perhaps you can use QTextCursor::mergeCharFormat(const QTextCharFormat & modifier)? http://doc.qt.io/qt-5/qtextcursor.html#mergeCharFormat
Example:
QTextCharFormat format;
format.setFontWeight(QFont::Bold);
cursor.mergeCharFormat(format);

QML Canvas/Context2D fillText() unexpected behaviour

I am trying to code a text editor from scratch in C++ using Qt/QML. For drawing the text I use a Canvas with a Context2D , which looks roughly like this:
function drawString(text, x, y, font) {
var ctx = getContext("2d");
ctx.font = font;
ctx.fillStyle = "black";
ctx.fillText(qsTr(text), x, y);
ctx.stroke();
}
In order to graphically represent a selected area, I want to invert the selecion, for instance place a black rectangle over an area and make the text white.
For this I will use ctx.globalCompositeOperation = "xor"
So the problem I ran into is: when I draw a text with the function above in black, and then afterwards paint the same text in the same location in white I would expect this canvas to be white again. Instead there is still some kind of outline of the text visible (like there is a shadow).
I already tried switching off all shadow parameters but it didn't solve my problem.
Here is a screenshot so you get a better idea of what it looks like:
Nevermind, I found the problem myself. The antialiasing property was set to true, which caused the effect. By setting it to false the text doesn't look as pretty but the shadow is gone.

Colouring a button in Qt

I'm trying to design a simple button (QPushButton or QToolButton, either can work) that would essentially be a rectangle of the colour it represents. Clicking it opens a QColorDialog, and selecting a colour in it repaints the button.
So, basically, something that will look like one of these:
I made a few attempts, none of which brought me the functionality I wanted.
Slot:
void MainWindow::OnButtonColorClick()
{
QColor initialColor = ui->buttonColor->palette().color(QPalette::Background);
QColor colorSelected = QColorDialog::getColor(initialColor, this);
if(colorSelected.isValid())
{
ui->buttonColor->setPalette(QPalette(colorSelected));
ui->buttonColor->setAutoFillBackground(true);
}
}
Attempt #1:
Set the Palette in the constructor:
ui->buttonCoulor->setPalette(QPalette(Qt::black));
Result: ordinary button before click, thin coloured contour after selection.
Attempt #2:
Add stylesheet:
background-color: rgb(0, 0, 0);
Result: black rectangle before click, black rectangle after selection.
I feel like I'm circling the drain. Essentially, how do I achieve:
?
Here is one way to accomplish the desired effect:
// Slot for the button
void MainWindow::on_button()
{
QColor color = QColorDialog::getColor();
QString s("background: #"
+ QString(color.red() < 16? "0" : "") + QString::number(color.red(),16)
+ QString(color.green() < 16? "0" : "") + QString::number(color.green(),16)
+ QString(color.blue() < 16? "0" : "") + QString::number(color.blue(),16) + ";");
button->setStyleSheet(s);
button->update();
}
Hope that helps.
use setStyleSheet with this style sheet:
border: 1px solid black;
background-color: #XXXXXX;
where XXXXXX is the value returned by QString::number(myColor.rgb(), 16).toUpper();
No need to set any other properties on the button. Leave them all as default and this will work.
This seems to be working just fine for me:
if(colorSelected.isValid())
{
ui->buttonColor->setPalette(QPalette(colorSelected));
}
As a follow-up to phyatt's answer, there's a shortcut to generate the XML-formatted color string:
QString s("background: " + color.name + ";");
button->setStyleSheet(s);

Lower text in a QMessagebox

I have a QMessagebox with a custom background image. Since there is some stuff on the top side of the background image I want to see, the text of the messagebox should be lowered. Does anybody know how I can do this? I already tried throwing in some white lines using br, so:
popup.setText("<font size =5 color =white ><br>""<br>""<br>""Are you sure you
want to erase the memory</font> ");
but this screws up the background picture. Is there any way I can move the "box" that contains the text to a lower position?
You could try to get the QMessageBox' layout, get the label which holds your text and increment the labels margin. This probably is a hack and might make your project unportable. Construct your QMessageBox, call hack and then exec the box.
void hack(QMessageBox* pMessageBox)
{
QGridLayout* grid = qobject_cast<QGridLayout*>(pMessageBox->layout());
if (grid)
{
QLabel* label = qobject_cast<QLabel*>((grid->itemAtPosition(0,1))->widget());
if (label)
{
label->setMargin(label->margin()+5); // whatever is suitable
}
}
}

How to make cxgrid row color transparent

I'm changing grid's row color to red according to my needs and when it gets colored the text is invisible, under backroung color...
How to change the text color or make it transparent ???
void __fastcall TfRegular::tvFillOutCustomDrawCell(TcxCustomGridTableView *Sender,
TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
bool &ADone)
{
TRect ARec;
ARec=AViewInfo->Bounds;
//AViewInfo->GridRecord->DisplayTexts[7]="+" + AViewInfo->GridRecord->DisplayTexts[7];
// ShowMessage(AViewInfo->GridRecord->Values[13]);
if (AViewInfo->GridRecord->Values[13]>1) {
ACanvas->Canvas->Brush->Color = clRed;
ACanvas->Canvas->Font->Color = clBlack;
ACanvas->Canvas->Pen->Color = clRed;
ACanvas->Canvas->FillRect(ARec);
SetBkMode(ACanvas->Canvas->Handle,TransparentColor);
//InflateRect(AViewInfo->Bounds,2,2);
//ACanvas->DrawTexT("",AViewInfo->Bounds,0);
ADone=true;
}
}
It was really easy than I thought.
I solved this problem using OnGetContentStyle event.

Resources