Qt QPlainTextEdit background - c++

I want to change the background color of a QPlainTextEdit, how do I do this?

Modify the palette of your plain text edit. Sample program:
#include <QApplication>
#include <QPlainTextEdit>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPlainTextEdit edit;
QPalette p = edit.palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::red);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::red);
edit.setPalette(p);
edit.show();
return app.exec();
}
Substitute whatever color you want, of course.

If QPlainTextEdit supports style sheets, you could do it like this:
myPlainTextEdit->setStyleSheet("background-color: yellow");
or
qApp->setStyleSheet("QPlainTextEdit {background-color: yellow}");

Slightly confusingly they call it role rather than colour/color.
https://doc.qt.io/qt-5/qwidget.html#setBackgroundRole
hint - if you can't find a function for a particular control, click on show inherited members - most general settings are in qWidget which is the basis for eveything drawn on screen.

May be you need to call QPlainTextEdit::setBackgroundVisible(true).

In order to modify the background, you need to modify the palette of your QPlainTextEdit and to set background visible:
myPlainTextEdit->setPalette(QPalette(/*Select the constructor you need*/));
myPlainTextEdit->setBackgroundVisible(true);

Related

Set the fill color (not the stroke color) for the QPainter in QT

How could I set the fill color (not the stroke color) for the QPainter in QT?
For example, I have a code which is responsible for filling the rectangle. It looks like:
painter.fillRect(fillRect, Qt::SolidPattern);
Where the type of painter is QPainter. Of course, I know that it is possible to specify the color in the case as a second parameter, but I have such a design in my program that it would be a lot better if I could set the painter fill color beforehand (by default the color is black).
I tried to use painter.setBackground(Qt::yellow);, but it did not help.
Hm. According to this we have:
Sets the painter's brush to the given brush.
The painter's brush defines how shapes are filled.
So, I would expect something like
QRect fillRect;
painter.setBrush(QBrush(Qt::yellow));
painter.fillRect(fillRect, Qt::SolidPattern);
to work. But it does not. What am I doing wrong?
After debugging it turns out that the setBrush method does not update the brush color at all:
The color rgb stays the same: (0, 0, 0).
fillRect() accepts a QBrush as a second parameter, so I could use it:
painter.fillRect(r, QBrush(Qt::yellow, Qt::SolidPattern));
Update:
#include <QApplication>
#include <QLabel>
#include <QPainter>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(128, 128);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
QRect r= pixmap.rect();
painter.setBrush(QBrush(Qt::yellow));
painter.fillRect(r, painter.brush());
painter.end();
QLabel w;
w.setPixmap(pixmap);
w.show();
return a.exec();
}

How to remove mainwindow titlebar permanently?

When I set mainwindow to fullscreen() , title bar disappears, thats what I want. But when a dialog is opened main window title bar appears again, which is undesirable in my case.I have tried setting several Qt::windowflags but they dont work.Any help will be greatly appreciated.
Quick answer here, you have to do something like this
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyMainWindow window;
window.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
window.show();
}
PS: What I gather from the net is that, the results are a bit ambiguous. Do let us know the result
To move the dialog in the centre:
1.Calling widget should pass the centre co-ordinates to the dialog window.
I have done it by passing co-ordinates by calling a dialog funuction.
In widget.cpp:
dialog->centre(this->width()/2,this->height()/2);
2.In the dialog.cpp :
`centre(int x,int y)
{
width =x; //store in some global variable
height=y;
}`
3.In the show event of dialog.cpp:
this->move(width,height);
Done. It will place the dialog in the centre of the widget.

How to put QImage with gray background inside QIcon?

I'm doing:
QIcon(QPixmap::fromImage(img));
and then putting it to QToolButton with setIcon and setIconSize
I need to have my img surrounded with gray color inside this icon - how can I implement this?
alike there:
Setting a Stylesheet should do the job for you..
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QToolButton b;
b.setStyleSheet("QToolButton { background-color: grey }");
QImage img("C:\\Users\\Administrator\\Desktop\\Icon.png");
b.setIcon( QPixmap::fromImage(img) );
b.show();
a.exec();
}
Try this out.
You can also play around with the QPalette of the button, that also provides you a very fine control over the way you want your widgets to look.

Transpaprent QLabel

#include <QtGui>
class Label : public QLabel
{
public:
Label(QWidget *parent =0) :
QLabel(parent)
{
resize(100, 100);
setText("hello");
show();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Label l;
return a.exec();
}
This outputs a label 'hello' with a background. I'm using Qt4. I want to make the background of this label completely transparent. But setWindowOpacity changes the whole widget transparency. I want the content as it is, but only the background to be transparent.
I found this as simpler....
QWidget::setAttribute(Qt::WA_TranslucentBackground);
You can use stylesheets to set the color and the alpha value of the background:
setStyleSheet("background-color: rgba(0,0,0,0%)");
In PyQt:
lbl.setAttribute(Qt.WA_TranslucentBackground, True)
If you define a QColor with alpha of 0 you will get background with transparent color, so for example:
QColor bg_color(255, 0, 0, 0);
QPalette p(l.palette());
p.setColor(QPalette::BackgroundColor, bg_color);
l.setPalette(p);
Which should make label's background whatever color transparent.
In my case with Qt5, the following worked:
movieLabel->setAutoFillBackground(false);
Tested both with Qt 5.11.1 and Qt 5.8.0.

Is it possible to remove the preview widget from a qt QColumnView?

I need to display a hierarchical set of data in a qt view. I'm using QColumnView to display the model. However, there is a feature such that the last column in the view will be relegated to a preview widget. Is it possible to hide this? Ex, something like view.setPreviewWidget( NULL ), although this breaks the program
EDIT : I should clarify that I'd like a way to hide the last column entirely, ie to have the last column in my view be the "leaves" of the model, and to not have a preview space
This will hide the button when it is clicked.
#include <QtGui/QApplication>
#include <QtGui/QColumnView>
#include <QtGui/QPushButton>
#include <QtGui/QFileSystemModel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QColumnView view;
QFileSystemModel model;
QPushButton button(&view);
button.setText("Click me");
QObject::connect(&button, SIGNAL(clicked()), &button, SLOT(hide()));
model.setRootPath("/");
view.setModel(&model);
view.setPreviewWidget(&button);
view.show();
return a.exec();
}
Notice that it will become hidden forever. You have to call show() if you want it to be displayed again.