C++ in QT: changing opacity buttons and labels - c++

I would like the make my buttons transparent, so that the user doesn't see them but only the background. Whenever I however set the opacity to 0, or change the background color. It does only change the edges. See image 1 (The middle button has a the stylesheet applied).
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setStyleSheet("background-image:url(:/images/gui_main.png)");
ui->alrm_dwn->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
connect(ui->alrm_dwn, SIGNAL(clicked()), this, SLOT(change_dwn()));
connect(ui->alrm_up, SIGNAL(clicked()), this, SLOT(change_up()));
}

found the answer, if you set the background of an widget using stylesheet like i did. All the object in the widget get the same background apparently.

Related

How to add a widget into central Widget using code in Qt

I need to add a widget to the form using code, but don't by use QHBoxLayout or QVBoxLayout.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fields[0] = new CustomLabel();
fields[0]->setText("Hello");
//how to show it
}
You can simply use setCentralWidget()
documentation of setCentralWidget()
like this ui->setCentralWidget(fields[0]); or maybe with this replacing ui
But, to displayed many widget you have to use a widget that contains the matrix of labels (widget). When you have the parent widget that contain the matrix display it with setCentralWidget()

QDialog - setting background image

How can I simply set background of my QDialog? I can easily set colour of background, but when i try use image, my background just goes black. I was trying to use: QPallete with QBrush, StyleSheets :(.
You can simply
MyDialog::MyDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
setStyleSheet( "background-image:url(image.jpg);" ); //this works
}
See also: Unable to set the background image in Qt Stylesheet

QTextEdit::setPalette is not updating the text color

I am changing the text color of two QTextEdit widgets along with quite a few other QLineEdits. This happens whenever I disable or enable the readonly property to make it better visually.
QPalette* disablePallete;
QPalette* enabledPallete;
disablePallete->setColor(QPalette::Text,Qt::darkGray);
enabledPallete->setColor(QPalette::Text,Qt::black);
// disable writing
ui->TextEdit->setPalette(*disablePallete);
// enable writing
ui->TextEdit->setPalette(*enablePallete);
This works perfectly on all of my LineEdits the instant I change the palette.
My problem is that the text inside the QTextEdits does not update immediately, and only does so when I click something inside the box, or edit the text. It glitches too and only updates where the cursor is moved to.
here is an example.
I got around it by doing this each time. It forces a repaint on everything.
ui->roomDescriptionTextEdit->append("");
ui->roomDescriptionTextEdit->undo();
Even though this works, I would like a less hacky way to update all of the text color on a QTextEdit.
Does anyone know of a solution?
I have only QTextEdit and QPushbutton on the form and it is working fine for me. If you won't find the error then please provide you full code.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(_handleClicked(bool)));
}
void MainWindow::_handleClicked(bool ok)
{
QPalette palette;
palette.setColor(QPalette::Text,Qt::darkGray);
ui->textEdit->setPalette(palette);
}
MainWindow::~MainWindow()
{
delete ui;
}
QTextEdit can handle rich text and QTextDocument controls that. Now properties of QTextEdit can control text which doesn't have rich text property set. If some part of text has some property set (color, bold, font, ...) this overrides default values from QTextEdit.
So most probably you have pasted rich text into text edit or performed some edit which set rich text values.

Qt Creator Align button float to the top

I have this gridlayout added onto my centralWidget of my UI in Qt Creator. And I wanted to add several buttons onto the gridlayout. My code is as follows:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *button1 = new QPushButton();
button1->setText("hello");
ui->gridLayout->addWidget(button1, 0,0);
}
MainWindow::~MainWindow()
{
delete ui;
}
Even though the button is added, it is seen appearing in the middle of the UI when the program is run. And there is a huge gap between the menu bar and button. How do i go about adding the button so that the button will flow nicely below the menu bar?
Please advice.
Thanks
The grid layout was placed in the middle of the UI in design mode, so anything you place inside will fill the fixed rectangle of the grid layout.
To make the grid layout fill the main window, right click the main window and click Lay out > Lay out vertically.
You can also fix the huge gap by placing a vertical spacer below the grid layout.
I guess this method work as well. By setting the alignment for the addWidget portion.
eg: ui->gridLayout->addWidget(button1, 0,0, Qt::AlignTop);

How can I restore the initial centralwidget of the QMainWindow?

I am kinda of new to Qt and decided to play around with it a bit so that one day I may be able to make some programs with a GUI. I've been playing around with windows and encountered a problem. I created two new QWidget Form classes in addition to the QMainWindow. With one of them, I was able to make a separate window popup and disappear. What I am trying to do with the second one is this...
I have a MainWindow with a button, and when that button is pressed, the CentralWidget changes to the QWidget with another button. Then, when I press a button on the new button, it restores the CentralWidget to the original one.
I did try storing the initial CentralWidget before changing it but I have yet to be able to successfully restore it. I do know about QStackedWidget, but I would like to know if there is another way of doing it. THANK YOU!
It seems Qt deletes child widget, when new one assigned as child widget. The fallowing code illustrates it and shows workaround
class QMyWidget: public QWidget
{
public:
~QMyWidget(){std::cout<<"Destroyed"<<std::endl;}
};
class QMyWidget2: public QWidget
{
public:
~QMyWidget2(){std::cout<<"Destroyed2"<<std::endl;}
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setCentralWidget(new QMyWidget());
this->setCentralWidget(new QWidget);
QWidget * w = new QMyWidget2();
this->setCentralWidget(w);
w->setParent(NULL);
this->setCentralWidget(new QWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
Do you want QStackedWidget? It's a but like QTabWidget but without the, er, tabs.