I'm working with the QTextEdit class.
I would like to set a predefinite color for my edit block.
When I open the textEdit and I begin to write something, I want to write the characters with a color that is different from zero.
I've tried to insert the following code, in the costructor of the class:
ui->textEdit->setTextColor(Qt::red)
but it doesn't work.
Thanks for the help
The setTextColor you have should work fine - QColor(255,0,0) and Qt::red are the same thing. Tried it and works fine.
OR maybe style sheets would help
qApp->setStyleSheet("QTextEdit { color: red; } ");
https://doc.qt.io/Qt-5/stylesheet-syntax.html
Related
Currently, the text in my application while using the QMessageBox text is a light grey and hard to read. To fix this, I tried to make the text black. I put the following in my style sheet to no avail. I also entered the following C++ code, but I rather use my stylesheet.
ExceptionDisplay
{
color: Black;
}
class ExceptionDisplay : public std::exception
{
public:
void ErrorMessageBox(QString methodName)
{
QMessageBox errorMsgBox;
...
errorMsgBox.setStyleSheet("color: Black");
}
}
I would like to use my stylesheet rather than putting it in code. What do I need to do to accomplish this? Thanks for the help in advance!
I now how to set value(number) but can i read that label in some variable. if can't read, in which similar widgets can i put value and read it. Any help would be halpful or someone has an other idea. Maybe like LCD number, spin box
Like this?
QString text = someLabel->text();
Here is the documentation. QLabel.text()
EDIT:
QString text = theLabel.text();
//or with pointer
QString text = theLabel->text();
have you tried :
QString s = YourQLabel->text();
This should work, I guess.
// Thelepaty mode on
You need to use editor widgets, for example QLineEdit or QTextEdit. You may customize it via QSS to look like a QLabel.
P.S. try to improve your English. It is hard to understand, what you are asking.
// Thelepaty mode off
Right now a read-only plain text edit has a white coloring, which does not make it obvious that it's read-only. I need to make my read-only plain text edit to be "gray". However, by gray I mean the default system color corresponding to it, instead of a hard-coded RGB(127, 127, 127).
The reason I can't just disable the widget is that I want the user to be able to select and copy the text. This is possible with read-only edit boxes but not if they are disabled.
Example screenshot of what I have in mind:
Very late response, but just looking for the same. Above solution works very nicely, but I guess the following code is more qt'isch:
// read-only PlainTextEdit with gray background
QColor mainWindowBgColor = palette().color(QPalette::Window);
// for the current widget
setStyleSheet(QString("QPlainTextEdit[readOnly=\"true\"] { background-color: %0 }").arg(mainWindowBgColor.name(QColor::HexRgb)));
// or application wide do:
qApp->setStyleSheet(QString("QPlainTextEdit[readOnly=\"true\"] { background-color: %0 }").arg(mainWindowBgColor.name(QColor::HexRgb)));
Advantage is that you need to do it only once, but gaining a consistent look and feel. Please pay attention of using the complex selector in the square brackets. Disadvantage is, but that's the same issue with the above code, that changing the OS design/profile will not have an effect until you re-execute the code (most probably doing a restart of your application).
- Its very sad that QtDesigner does not offer a simple way to do that - or I did not yet figure out how to do it.
And finally as application wide one-liner for all elements having a readOnly attribute set:
qApp->setStyleSheet(QString("[readOnly=\"true\"] { background-color: %0 }").arg(qApp->palette().color(QPalette::Window).name(QColor::HexRgb)));
After some Googling and some experimenting, I found out how to do it:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPalette readOnlyPalette = ui->plainTextEdit->palette();
QColor mainWindowBgColor = palette().color(QPalette::Window);
readOnlyPalette.setColor(QPalette::Base, mainWindowBgColor);
ui->plainTextEdit->setPalette(readOnlyPalette);
}
I try to ask not to localized, because I think many other wxWidgets users will stumble about this problem or a similar one.
I try to get a wxStaticBitmap (original a PNG-Image) in a GUI, I will try to model it:
text text text
text text text
IMAGE
text
text
How I can do this? When I try:
wxBitmap bild(_T("Bild.png"), wxBITMAP_TYPE_PNG);
wxStaticBitmap *image = new wxStaticBitmap(this, wxID_ANY,
wxBitmap("Bild.png", wxBITMAP_TYPE_PNG),
wxPoint(100,100),
wxSize(bild.GetWidth(), bild.GetHeight()));
The image will take over the hole window, even the wxNotebook, which contains the wxPanel, will disabled and I have only the image, but nothing else...
I want to get the image in the third row of the VerticalBox. Is there any way to make this happend?
-Casisto
(wxW: 2.9.4; C++-Compiler: g++)
There is nothing special about wxStaticBitmap and you absolutely don't need to put it inside a panel. You do need to create it with the correct parent, if this in your example refers to wxFrame, it's not going to work if you then put it inside a wxPanel sizer, for example.
P.S. Do get rid of _T in your code, it's completely unnecessary in 2.9+. You also probably don't want to load the bitmap twice...
Ok, i found the answer myself:
I've to use a "subpanel" which will be the parent of the StaticBitmap
so it's this structur:
panel << vbox << Text
vbox << panel_2 << Image
vbox << Text
I want to be able to display formatted text inside a message box (like bold text, bullet points, italics, etc.).
I came across this wonderful article but can't seem to get it to work. I am using the demo application at that same link.
Can someone please help me out? I have tried debugging/understanding that code in vain.
Constraints: (not my choice)
Has to be compatible with Windows XP.
I'm using Visual C++ 6.
How it is supposed to display:
How it actually displays:
Simply create a dialog bow with a RichEdit2 control...
In InitInstance, add the follwing call:
// Init RichEdit Library
AfxInitRichEdit2();
In your dialog box, create a variable to the RichEdit control and update it as:
// Turn Word Wrap on (based on window width)
m_RichEditMsg.SetTargetDevice( NULL, 0);
// Set Base Text
strText = "{\\rtf1\\ansi\\fs20 ";
strText += "{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red255\\green0\\blue0;}";
strText += "{\\f1\\cb1\\cf2\\b Main Title} \\par\\par \\fs18 Other text to add {\\b In Bold} no more in bolb ... \\par";
str.Format( "\\par Id: {\\b %s}", m_strProgId);
strText += str;
strText+= "\\par \\par {\\f1 \\b Please Confirm ...} \\par}";
// Update Controls
m_RichEditMsg.SetWindowText( strText);
Simply build your own message and you get bold, color, ...
I have solved this problem thanks to the very helpful suggestions of DavidK (see comments on the question). The FIX for Windows 2000 comment fixed this neatly.