C++ QT Add a widget to scroll area without layout - c++

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!

Related

Qt : Changing color of an icon used through FontAwesome in Qt when accessed through Unicode.

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;
}

QtWebEngine display black block when I called QWidget::winId()

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QWebEngineView>
#include <qDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
#if 1
auto btn = new QPushButton;
ui->gridLayout->addWidget(btn);
qDebug()<<btn->winId();
#endif
auto web = new QWebEngineView;
ui->gridLayout->addWidget(web);
web->load(QUrl("http://www.google.com"));
}
MainWindow::~MainWindow()
{
delete ui;
}
That's the whole code.
Windows 10 , Qt 5.5 .
When I turn on the switch, winId() would be called, then the QtWebEngine can not work rightly.
What should I do ?
Don't call winId() until the widget is visible. You could set an eventFilter that is activated on QEvent::Show, for further information see http://doc.qt.io/qt-5/qobject.html#installEventFilter

Insert QSplitter in QStackedWidget

I have a QStackedWidget with a custom widget in it.
//mainwindow.h
private:
CentralWidget m_centralWidget;
//mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->stackedWidget->addWidget(&m_centralWidget);
}
Now i want to create a QSplitter, fill the Splitter with content and add this Splitter to the StackedWidget.
CentralWidget::CentralWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::CentralWidget)
{
ui->setupUi(this);
connect(ui->pbAddSplit, SIGNAL(pressed()), this, SLOT(addSplit()));
}
void CentralWidget::addSplit()
{
QWidget* parent = parentWidget();
QStackedWidget* stackedWidget = qobject_cast<QStackedWidget*>(parent);
if(!stackedWidget) {
return;
}
QSplitter* splitter = new QSplitter(Qt::Vertical);
splitter->addWidget(new QLabel("Banana"));
splitter->addWidget(this);
int nIndex = stackedWidget->addWidget(splitter);
stackedWidget->setCurrentIndex(nIndex);
}
The result should be a QLabel("Banana") as one Widget of the splitter and the CentralWidget as the other splitter-widget.
But i just get the QLabel("Banana") - SplitterHandle and the CentralWidget got lost somehow...
splitter->addWidget(this) is obviously wrong...
Replacing it with splitter->addWidget(new QLabel("Cherry")) will result in a correct SplitterWidget...
The problem seems to be the parenting, but i cant figure it out.
Any suggestions would be most welcome!

QGraphicsDropShadowEffect works only for one item on ui, although I used it everywhere

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.

How to expand widget boundaries in QTabWidget

I would like to eliminate the border from a QTabWidget as shown in the diagram. The bottom half of the tab labels should occlude the widgets where they intersect. Is this possible? Code follows:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QGraphicsScene"
#include "QTableWidget"
#include "QGraphicsView"
#include "QTextEdit"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsView* left = new QGraphicsView(new QGraphicsScene);
QTextEdit* right = new QTextEdit;
QTabWidget* tab = new QTabWidget(this);
tab->insertTab(0, left, "Left");
tab->insertTab(1, right, "Right");
setCentralWidget(tab);
}
MainWindow::~MainWindow()
{
delete ui;
}
You could use QWidget::layout() and QLayout.setContentsMargins ( int left, int top, int right, int bottom ) methods. For example:
ui->centralwidget->layout()->setContentsMargins(9,9,9,9);
tab->layout()->setContentsMargins(9,9,9,9);
Should make the change you want. But I'd suggest creating and modifying forms in in Qt designer rather than in code.