setAutoFillBackgroundRole not working
-
I check the autoFillBackground box and
ui->canvas->setAutoFillBackground(QPalette::Base);but I do not get the canvas filled with white.
Canvas is a new class and in Designer I added a widget, promoted it to MyCanvas and added the line above to MainWindow. I have done this more times than I can remember, but this is the first time it has failed. Can some one suggest how I can find out why this fail? -
Update. I created a new UI using the same steps as before and I works fine! Sometimes Designer drives me crazy, especially when layouts are involved. However, for now, problem solved. Thanks for your input. However, I would still like that reference to programmatically creating a UI with QMainWindow.
-
@ofmrew
Ok super.You mean a QMainWindow based one from code ?
#include <QApplication> #include <QMainWindow> #include <QMenuBar> #include <QStatusBar> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow MainWindow; MainWindow.setObjectName(QString::fromUtf8("MainWindow")); MainWindow.resize(800, 600); // mainwin has a special area called centralwidget where you put stuff QWidget *centralwidget = new QWidget(&MainWindow); MainWindow.setCentralWidget(centralwidget); // and a menu bar QMenuBar *menubar = new QMenuBar(&MainWindow); MainWindow.setMenuBar(menubar); // and a status bar QStatusBar *statusbar = new QStatusBar(&MainWindow); MainWindow.setStatusBar(statusbar); MainWindow.show(); return a.exec(); }
-
@mrjj I am using your code in QtCreator Widget Application, not Console Application because when I tried that the includes could not be found. I have searched the documentation and I have this :
QWidget *centralwidget = new QWidget(&MainWindow); MainWindow.setCentralWidget(centralwidget); QVBoxLayout * centraLWidgetLayout = new QVBoxLayout(centralwidget); QHBoxLayout * buttonLayout = new QHBoxLayout(); QWidget * mainWindow = new QWidget(centralwidget); QPushButton * button1 = new QPushButton("Button 1"); QPushButton * button2 = new QPushButton("Button 2"); buttonLayout->addWidget(button1); buttonLayout->addWidget(button2);
I know that there must be a layout associated with the central widget but how do I get centraLWidgetLayout associated with the central widget and I assume that buttonLayout must be associated with some widget, but what is that widget? I can add my buttons to the buttonLayout and I what that layout and MyCanvas widget In the centraLWidgetLayout.
This why I was looking for an example that starts with QMainWindow and not QWidget. The documentation says what happens, but not how to get what you want to happen. Help.
-
I finally got it:
#include "mainwindow.h" #include <QMainWindow> #include <QMenuBar> #include <QStatusBar> #include <QHBoxLayout> #include <QVBoxLayout> #include <QWidget> #include <QPushButton> #include "mycanvas.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow MainWindow; MainWindow.setObjectName(QString::fromUtf8("MainWindow")); MainWindow.resize(800, 600); // mainwin has a special area called centralwidget where you put stuff QWidget *centralwidget = new QWidget(&MainWindow); MainWindow.setCentralWidget(centralwidget); QVBoxLayout * centraLWidgetLayout = new QVBoxLayout(centralwidget); MyCanvas * canvas = new MyCanvas(centralwidget); canvas->setAutoFillBackground(true); canvas->setBackgroundRole(QPalette::Base); QHBoxLayout * buttonLayout = new QHBoxLayout(); QPushButton * button1 = new QPushButton("Button 1"); QPushButton * button2 = new QPushButton("Button 2"); buttonLayout->addWidget(button1); buttonLayout->addWidget(button2); centraLWidgetLayout->addWidget(canvas); centraLWidgetLayout->addLayout(buttonLayout); // and a menu bar QMenuBar *menubar = new QMenuBar(&MainWindow); MainWindow.setMenuBar(menubar); // and a status bar QStatusBar *statusbar = new QStatusBar(&MainWindow); MainWindow.setStatusBar(statusbar); MainWindow.show(); return a.exec(); }
Thanks mrjj.
I need to add horizontal spacers.
Easier than I thought it would be.Now I need to find out why the .h and .ccp file were not found when I tried console mode.
-
The following is a updated version. The last version did not do anything so I add signals and slots so it would be useful.
#include "mainwindow.h" #include <QMainWindow> #include <QMenuBar> #include <QStatusBar> #include <QHBoxLayout> #include <QVBoxLayout> #include <QWidget> #include <QPushButton> #include "mycanvas.h" #include <QDebug> #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow MainWindow; MainWindow.setObjectName(QString::fromUtf8("MainWindow")); MainWindow.setWindowTitle("My Try"); MainWindow.resize(800, 600); QWidget *centralwidget = new QWidget(&MainWindow); MainWindow.setCentralWidget(centralwidget); QVBoxLayout * centraLWidgetLayout = new QVBoxLayout(centralwidget); MyCanvas * canvas = new MyCanvas(centralwidget); canvas->setAutoFillBackground(true); canvas->setBackgroundRole(QPalette::Base); QHBoxLayout * buttonLayout = new QHBoxLayout(); QPushButton * button1 = new QPushButton("Button 1"); QPushButton * button2 = new QPushButton("Button 2"); buttonLayout->addWidget(button1); buttonLayout->addWidget(button2); buttonLayout->insertSpacing(1,30); centraLWidgetLayout->addWidget(canvas); centraLWidgetLayout->addLayout(buttonLayout); // and a menu bar QMenuBar *menubar = new QMenuBar(&MainWindow); MainWindow.setMenuBar(menubar); // and a status bar QStatusBar *statusbar = new QStatusBar(&MainWindow); MainWindow.setStatusBar(statusbar); MainWindow.connect(button1, &QPushButton::clicked,[statusbar](){statusbar->showMessage("clicked 1");}); MainWindow.show(); return a.exec(); }
However, after adding the spacer to separate the two buttons and looking at the work still to be done, really makes me appreciate Designer, even with all it quirks. Hope this might help someone else.