I'm trying to find a widget that I added to the screen and do some manipulations with it, but the list of children is empty every time. What am I doing wrong?
Here is the code (to run it, create a new clean project and replace the text in MainWindow.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QLabel>
#include <QLayout>
#include <QList>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel* wid = new QLabel("hello");
ui->centralWidget->layout()->addWidget(wid);
QList<QLabel*> list = ui->centralWidget->findChildren<QLabel*>();
qDebug() << list.isEmpty();
}
MainWindow::~MainWindow()
{
delete ui;
}
so I an new to C++ and QT and I am trying to add a widget to a scroll area but without a layout as I want the widgets to be click and dragged.
Here's my code (main window.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "player_window.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
player_window *player = new player_window(ui->scrollArea_WidgetContents);
player->setGeometry(50,50,450,130);
ui->scrollArea_WidgetContents->layout()->addWidget(player);
}
Thanks!
I have written a code to print an Icon accessed from FontAwesome installed in the system. I would like to change the color for the Icon printed on the Screen.I have tried using QPixmap and QIcon,but to no avail.Attached the output :
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QPushButton>
#include <QGridLayout>
#include <QWidget>
#include <QLabel>
#include "qfonticon.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget *centralWidget;
QGridLayout *gridLayout;
centralWidget = new QWidget(this);
gridLayout = new QGridLayout( centralWidget );
QFontIcon::addFont("/usr/share/fonts/fontawesome-webfont.ttf");
QIcon icon = QFontIcon::icon(0xf2e0,QColor(1,0,1,255));
//QFontIconEngine::addFile("/usr/share/fonts/fontawesome-webfont.ttf");
//QPixmap pix = QFontIconEngine::;
QPushButton *b = new QPushButton();
//QLabel *l = new QLabel();
b->setIcon(icon);
b->setIconSize(QSize(75,75));
//l->setPixmap(pix);
gridLayout->addWidget(b);
//gridLayout->addWidget(l);
this->setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
I want to have a QScrollArea inside QGroupBox, so when I add new widgets to group box its size stays the same, but I have scroll bars instead of resizing group box itself.
Here's my code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtGui>
#include <QLayout>
#include <QScrollArea>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGroupBox *box = new QGroupBox(QObject::tr("Example"));
QScrollArea *sa = new QScrollArea;
QGridLayout *gridLayout = new QGridLayout;
QPushButton *b1 = new QPushButton("A");
QPushButton *b2 = new QPushButton("B");
QPushButton *b3 = new QPushButton("C");
QPushButton *b4 = new QPushButton("D");
QPushButton *b5 = new QPushButton("E");
QPushButton *b6 = new QPushButton("F");
QPushButton *b7 = new QPushButton("F");
QPushButton *b8 = new QPushButton("F");
QPushButton *b9 = new QPushButton("F");
// addWidget(*Widget, row, column, rowspan, colspan)
// 0th row
gridLayout->addWidget(b1,0,0,1,1);
gridLayout->addWidget(b2,0,1,1,1);
gridLayout->addWidget(b3,0,2,1,1);
// 1st row
gridLayout->addWidget(b4,1,0,1,1);
// 2nd row with 2-column span
gridLayout->addWidget(b5,2,0,1,2);
// 3rd row with 3-column span
gridLayout->addWidget(b6,3,0,1,3);
gridLayout->addWidget(b7,4,0,1,3);
gridLayout->addWidget(b8,5,0,1,3);
gridLayout->addWidget(b9,6,0,1,3);
box->setLayout(gridLayout);
sa->setWidget(box);
setCentralWidget(sa);
}
MainWindow::~MainWindow()
{
delete ui;
}
What I have now is that every time I add a new QPushButton, QGroupBox resizes, no matther there is a QScrollArea. What should I change to have the behaviour I want? Is it possible?
That's because you are putting the groupbox inside the scroll area. Scroll area doesn't restrict its childrens size.
You should do the opposite, put scrollarea inside group box. Here is how;
QWidget* sw = new QWidget();
sw->setLayout(gridLayout);
sa->setWidget(cont);
QVBoxLayout* bl = new QVBoxLayout(box);
bl->addWidget(sa);
setCentralWidget(box);
Note that if you are using toggle buttons (such as radio button) they will not act as a group. Because technically they are not in the same QGroupBox any more - they are inside the scroll area. You can provide group behavior using a QButtonGroup instance.
I'm using QGraphicsDropShadowEffect to make my GUI look pretier. Minimal working sample:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsDropShadowEffect *g = new QGraphicsDropShadowEffect(this);
ui->pushButton->setGraphicsEffect(g);
ui->pushButton_2->setGraphicsEffect(g);
ui->pushButton_3->setGraphicsEffect(g);
}
MainWindow::~MainWindow()
{
delete ui;
}
As you see, I have 3 buttons and want to have a fancy shadow at the top of every button. Although I set graphics effect on every button it can be seen only on the last button, heres the image:
How can I improve it and what is the cause?
This works:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsDropShadowEffect *g1 = new QGraphicsDropShadowEffect(this);
QGraphicsDropShadowEffect *g2 = new QGraphicsDropShadowEffect(this);
QGraphicsDropShadowEffect *g3 = new QGraphicsDropShadowEffect(this);
ui->pushButton->setGraphicsEffect(g1);
ui->pushButton_2->setGraphicsEffect(g2);
ui->pushButton_3->setGraphicsEffect(g3);
}
MainWindow::~MainWindow()
{
delete ui;
}
but seem not to be the best solution I can have.
It's the normal behavior of the function you are calling
see documentation
http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setGraphicsEffect
If effect is the installed on a different item, setGraphicsEffect()
will remove the effect from the item and install it on this item.