I'm inside a class which deals with a simple window (I'm starting with Qt) but when I try to add a new Widget (from a slot of the custom class) to this window, it doesn't appear on the window. I use QTextStream which shows that the slot is correctly called and the random coordinates generated are rights so I totally do not understand why there is nothing new on my window. Moreover, the first time the slot is called as a function in the ButtonsWindow::ButtonsWindow(QWidget *parent) : QWidget(parent) {}, the widget appears correctly... Do you have any ideas ?
Here is my .h code :
#ifndef BUTTONSWINDOW_H
#define BUTTONSWINDOW_H
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <cstdlib>
#include <ctime>
#include <QDebug>
#include <QVector>
class ButtonsWindow : public QWidget
{
Q_OBJECT
public:
explicit ButtonsWindow(QWidget *parent = nullptr);
private:
QPushButton *testButton;
QVector<QWidget*> buttonsArray;
signals:
public slots:
void createNewButton();
};
#endif // BUTTONSWINDOW_H
Here is my .cpp class code :
#include "buttonswindow.h"
ButtonsWindow::ButtonsWindow(QWidget *parent) : QWidget(parent)
{
setFixedSize(1000, 300);
createNewButton();
}
void ButtonsWindow::createNewButton()
{
QWidget *mainWidget = new QWidget(this);
QPalette pal = palette();
pal.setColor(QPalette::Background, Qt::lightGray);
mainWidget->setAutoFillBackground(true);
mainWidget->setPalette(pal);
QGridLayout *layout = new QGridLayout;
QLabel *text = new QLabel("What to do?");
text->setCursor(Qt::WhatsThisCursor);
QPushButton *addWidget = new QPushButton("Create a new Button");
QObject::connect(addWidget, SIGNAL(pressed()), this, SLOT(createNewButton()));
addWidget->setCursor(Qt::CrossCursor);
QPushButton *removeWidget = new QPushButton("Remove an existing random Button");
removeWidget->setCursor(Qt::ForbiddenCursor);
layout->addWidget(text, 0, 0, 1, 2, Qt::AlignCenter);
layout->addWidget(addWidget, 1, 0, 1, 1, Qt::AlignCenter);
layout->addWidget(removeWidget, 1, 1, 1, 1, Qt::AlignCenter);
mainWidget->setLayout(layout);
int nX = (rand() % this->width() - 2 * mainWidget->width());
nX += mainWidget->width();
int nY = (rand() % this->height() - 2 * mainWidget->height());
nY += mainWidget->height();
mainWidget->move(nX, nY);
QTextStream(stdout) << "check " << nX << " : : " << nY;
buttonsArray.append(mainWidget);
}
Finnaly, here is my main.cpp code :
#include <QApplication>
#include <QPushButton>
#include "buttonswindow.h"
#include <QGridLayout>
using namespace std;
int main(int argc, char *argv[]) {
srand(time(0));
QApplication app(argc, argv);
ButtonsWindow window;
window.show();
return app.exec();
}
From the documentation...
If you add a child widget to an already visible widget you must
explicitly show the child to make it visible.
So you just need to add
mainWidget->show();
at the end of your ButtonsWindow::createNewButton implementation.
Related
Consider following main.cpp (the only file of the whole Qt-project):
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QObject>
#pragma once
class collectedFunctions : public QObject
{
Q_OBJECT
public:
collectedFunctions(QObject* parent = 0) {};
~collectedFunctions() {};
public slots:
void setFunc() {
//Calculate 2+2 and change text of the label accordingly
}
};
#include "main.moc"
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QWidget window;
window.resize(200, 200);
window.setWindowTitle("Test-GUI");
QVBoxLayout layout(&window);
QPushButton button(&window);
button.setText("Test");
button.show();
QLabel *label = new QLabel(&window);
label->setText("Hello");
collectedFunctions testingFuncs;
QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);
layout.addWidget(&button);
layout.addWidget(label);
window.show();
return app.exec();
}
My goal with this code is to create a slot that runs a function which the programmer defines (similar to the pyqt-implementation of the signals and slots system). The code above successfully compiles, not throwing any errors and so far meeting my expectations. Yet as soon I want to manipulate a widget (which in this case is the label), the setfunc-slot does not find the defined label. How can I manipulate a widget using this slot (e.g. using setText() at a label or addItems() at a combobox) or generally have them being recognized in the slot.
Update:
Thanks to the solution of this question, I figured that I could simply use a lambda instead of the Q_OBJECT makro, so I removed the header-code and then changed the connect from the Button to following:
QObject::connect(&button, &QPushButton::clicked, [&label](){
int num = 2 + 2;
string text = "2+2 = ";
text += std::to_string(num);
QString qtext = QString::fromStdString(text);
label->setText(qtext); });
you shouldn't use the Q_OBJECT macro in main.cpp.
The Q_OBJECT macro must appear in the private section of a class
definition that declares its own signals and slots or that uses other
services provided by Qt's meta-object system.
The moc tool reads a C++ header file. If it finds one or more class
declarations that contain the Q_OBJECT macro, it produces a C++ source
file containing the meta-object code for those classes. Among other
things, meta-object code is required for the signals and slots
mechanism, the run-time type information, and the dynamic property
system.
The C++ source file generated by moc must be compiled and linked with
the implementation of the class.
When you have your class defined in .cpp file instead of .h file moc fails to process it properly.
you need a separate file for class:
in collectedfunctions.h
#pragma once
#include <QObject>
class collectedFunctions: public QObject
{
Q_OBJECT
public:
collectedFunctions(QObject *parent = 0);
~collectedFunctions();
public slots:
void setFunc();
};
and in collectedfunctions.cpp
#include "collectedfunctions.h"
#include <QDebug>
collectedFunctions::collectedFunctions(QObject *parent)
{
}
collectedFunctions::~collectedFunctions()
{
}
void collectedFunctions::setFunc()
{
qDebug() << "2+2 = " << 2 + 2;
}
and in your main.cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include "collectedfunctions.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(200, 200);
window.setWindowTitle("Test-GUI");
QVBoxLayout layout(&window);
QPushButton button(&window);
button.setText("Test");
button.show();
QLabel *label = new QLabel(&window);
label->setText("Hello");
collectedFunctions testingFuncs;
QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);
layout.addWidget(&button);
layout.addWidget(label);
window.show();
return app.exec();
}
don't forget to add QT +=core gui widgets in your .pro file.
Result:
If you want to change QLabel Text :
in collectedfunctions.h
#pragma once
#include <QObject>
class collectedFunctions: public QObject
{
Q_OBJECT
public:
explicit collectedFunctions(QObject *parent = 0);
~collectedFunctions();
signals:
void updateLable(int num);
public slots:
void setFunc();
private:
int num = 0;
};
and in collectedfunctions.cpp
#include "collectedfunctions.h"
#include <QDebug>
collectedFunctions::collectedFunctions(QObject *parent)
{
}
collectedFunctions::~collectedFunctions()
{
}
void collectedFunctions::setFunc()
{
qDebug() << "2+2 = " << 2 + 2;
num++;
emit updateLable(num);
}
and in your main.cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include "collectedfunctions.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(200, 200);
window.setWindowTitle("Test-GUI");
QVBoxLayout layout(&window);
QPushButton button(&window);
button.setText("Test");
button.show();
QLabel *label = new QLabel(&window);
label->setText("Hello");
collectedFunctions testingFuncs;
QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);
QObject::connect(&testingFuncs, &collectedFunctions::updateLable, [&label](int num)
{
qDebug() << "Number = " << num;
label->setText(QString::number(num));
});
layout.addWidget(&button);
layout.addWidget(label);
window.show();
return app.exec();
}
you can see:
I am working on a project in which I am supposed to make two buttons(These buttons are named as Power calculation and Power calculation 2 ). When the user clicks on the buttons, a sliding area opens up under the buttons.
I have written this code successfully, but my problem is the layout of these buttons and the sliding area bellow each button(The buttons are defined in separate classes. The button named as "Power calculation" is defined in "Section.cpp" and the button named as "Power calculation 2" is defined in "Section2.cpp").
My goal is to set the buttons in the same row (the button named as "power calculation" must be on the left side of the button named as "Powercalculation2" and both of them are supposed to be in the same row). I use QGridLayout to set the layouts to my elements, but I can't get results. I have also tried to use move() and setGeometry() , but I could not get what I want to do.
I would appreciate if someone could help me in this matter.
The picture below is what I get after running my program:
But the result that I want to get is the picture below:
MY project ui file picture:
Here is my code:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Section.h:
#ifndef SECTION_H
#define SECTION_H
#include <QFrame>
#include <QGridLayout>
#include <QParallelAnimationGroup>
#include <QScrollArea>
#include <QGridLayout>
#include <QParallelAnimationGroup>
#include <QScrollArea>
#include <QToolButton>
#include <QWidget>
#include "Section2.h"
class Section : public QWidget {
Q_OBJECT
private:
QGridLayout* mainLayout;
QToolButton* toggleButton;
QFrame* headerLine;
QParallelAnimationGroup* toggleAnimation;
QScrollArea* contentArea;
int animationDuration;
public slots:
void toggle(bool collapsed);
public:
explicit Section(const QString & title = "", const int animationDuration = 100, QWidget* parent =
0);
void setContentLayout(QLayout & contentLayout);
};
#endif // SECTION_H
Section2.h:
#ifndef SECTION2_H
#define SECTION2_H
#include <QFrame>
#include <QGridLayout>
#include <QParallelAnimationGroup>
#include <QScrollArea>
#include <QGridLayout>
#include <QParallelAnimationGroup>
#include <QScrollArea>
#include <QToolButton>
#include <QWidget>
class Section2 : public QWidget {
Q_OBJECT
private:
QGridLayout* mainLayout;
QToolButton* toggleButton;
QFrame* headerLine;
QParallelAnimationGroup* toggleAnimation;
QScrollArea* contentArea;
int animationDuration;
public slots:
void toggle(bool collapsed);
public:
explicit Section2(const QString & title = "", const int animationDuration = 100, QWidget* parent =
0);
void setContentLayout(QLayout & contentLayout);
};
#endif // SECTION_H
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Section.h"
#include "Section2.h"
#include <QLabel>
#include <QPushButton>
#include <QBoxLayout>
#include <QLineEdit>
#include <QGridLayout>
#include <QComboBox>
#include <QDebug>
#include <QRegularExpressionValidator>
#include <QValidator>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Section *section = new Section("Section", 100, ui->centralWidget);
ui->centralWidget->layout()->addWidget(section);
Section2 *section2 = new Section2("Section2", 100, ui->centralWidget);
ui->centralWidget->layout()->addWidget(section2);
QVBoxLayout* anyLayout = new QVBoxLayout();
QLabel *lblV = new QLabel("insert voltage",section);
anyLayout->addWidget(lblV);
QLineEdit *lineV = new QLineEdit("",section);
anyLayout->addWidget(lineV);
QLabel *lblI = new QLabel("insert current",section);
anyLayout->addWidget(lblI);
QLineEdit *lineI = new QLineEdit("",section);
anyLayout->addWidget(lineI);
QPushButton *btn = new QPushButton("power: ",section);
anyLayout->addWidget(btn);
QLabel *lbl = new QLabel("",section);
lbl->setStyleSheet("background-color: yellow");
anyLayout->addWidget(lbl);
QVBoxLayout* anyLayout2 = new QVBoxLayout();
QLabel *lblV2 = new QLabel("insert voltage",section2);
anyLayout2->addWidget(lblV2);
QLineEdit *lineV2 = new QLineEdit("",section2);
anyLayout2->addWidget(lineV2);
QLabel *lblI2 = new QLabel("insert current",section2);
anyLayout2->addWidget(lblI2);
QLineEdit *lineI2 = new QLineEdit("",section2);
anyLayout2->addWidget(lineI2);
QPushButton *btn2 = new QPushButton("power: ",section2);
anyLayout2->addWidget(btn2);
QLabel *lbl2 = new QLabel("",section2);
lbl2->setStyleSheet("background-color: pink");
anyLayout2->addWidget(lbl2);
section->setContentLayout(*anyLayout);
section2->setContentLayout(*anyLayout2);
}
MainWindow::~MainWindow()
{
delete ui;
}
Section.cpp:
#include <QPropertyAnimation>
#include "Section.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
Section::Section(const QString & title, const int animationDuration, QWidget* parent)
: QWidget(parent), animationDuration(animationDuration)
{
toggleButton = new QToolButton(this);
headerLine = new QFrame(this);
toggleAnimation = new QParallelAnimationGroup(this);
contentArea = new QScrollArea(this);
mainLayout = new QGridLayout(this);
toggleButton->setStyleSheet("QToolButton {border: none;}");
toggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toggleButton->setArrowType(Qt::ArrowType::RightArrow);
toggleButton->setText("Power calculation");
toggleButton->setCheckable(true);
toggleButton->setChecked(false);
headerLine->setFrameShape(QFrame::HLine);
headerLine->setFrameShadow(QFrame::Sunken);
headerLine->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
contentArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// start out collapsed
contentArea->setMaximumHeight(0);
toggleAnimation->addAnimation(new QPropertyAnimation(contentArea, "maximumHeight"));
mainLayout->setVerticalSpacing(0);
mainLayout->addWidget(toggleButton, 0, 0, 1, 1);
mainLayout->addWidget(headerLine, 0, 1, 1, 1);
mainLayout->addWidget(contentArea, 3, 0, 1, 3);
setLayout(mainLayout);
connect(toggleButton, &QToolButton::toggled, this, &Section::toggle);
}
void Section::toggle(bool collapsed) {
toggleButton->setArrowType(collapsed ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow);
toggleAnimation->setDirection(collapsed ? QAbstractAnimation::Forward :
QAbstractAnimation::Backward);
toggleAnimation->start();
}
void Section::setContentLayout(QLayout & contentLayout)
{
delete contentArea->layout();
contentArea->setLayout(&contentLayout);
const auto collapsedHeight = sizeHint().height() - contentArea->maximumHeight();
auto contentHeight = contentLayout.sizeHint().height();
for (int i = 0; i < toggleAnimation->animationCount() - 1; ++i)
{
QPropertyAnimation* SectionAnimation = static_cast<QPropertyAnimation *>(toggleAnimation->animationAt(i));
SectionAnimation->setDuration(animationDuration);
SectionAnimation->setStartValue(collapsedHeight);
SectionAnimation->setEndValue(collapsedHeight + contentHeight);
}
QPropertyAnimation* contentAnimation = static_cast<QPropertyAnimation *>(toggleAnimation->animationAt(toggleAnimation->animationCount() - 1));
contentAnimation->setDuration(animationDuration);
contentAnimation->setStartValue(0);
contentAnimation->setEndValue(contentHeight);
}
Section2.cpp:
#include <QPropertyAnimation>
#include "Section2.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
Section2::Section2(const QString & title, const int animationDuration, QWidget* parent)
: QWidget(parent), animationDuration(animationDuration)
{
toggleButton = new QToolButton(this);
headerLine = new QFrame(this);
toggleAnimation = new QParallelAnimationGroup(this);
contentArea = new QScrollArea(this);
mainLayout = new QGridLayout(this);
toggleButton->setStyleSheet("QToolButton {border: none;}");
toggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toggleButton->setArrowType(Qt::ArrowType::RightArrow);
toggleButton->setText("Power calculation 2");
toggleButton->setCheckable(true);
toggleButton->setChecked(false);
headerLine->setFrameShape(QFrame::HLine);
headerLine->setFrameShadow(QFrame::Sunken);
headerLine->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
contentArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// start out collapsed
contentArea->setMaximumHeight(0);
toggleAnimation->addAnimation(new QPropertyAnimation(contentArea, "maximumHeight"));
mainLayout->setVerticalSpacing(0);
mainLayout->addWidget(toggleButton, 0, 0, 1, 1);
mainLayout->addWidget(headerLine, 0, 1, 1, 1);
mainLayout->addWidget(contentArea, 3, 0, 1, 3);
setLayout(mainLayout);
connect(toggleButton, &QToolButton::toggled, this, &Section2::toggle);
}
void Section2::toggle(bool collapsed) {
toggleButton->setArrowType(collapsed ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow);
toggleAnimation->setDirection(collapsed ? QAbstractAnimation::Forward :
QAbstractAnimation::Backward);
toggleAnimation->start();
}
void Section2::setContentLayout(QLayout & contentLayout)
{
delete contentArea->layout();
contentArea->setLayout(&contentLayout);
const auto collapsedHeight = sizeHint().height() - contentArea->maximumHeight();
auto contentHeight = contentLayout.sizeHint().height();
for (int i = 0; i < toggleAnimation->animationCount() - 1; ++i)
{
QPropertyAnimation* SectionAnimation = static_cast<QPropertyAnimation *>(toggleAnimation-
>animationAt(i));
SectionAnimation->setDuration(animationDuration);
SectionAnimation->setStartValue(collapsedHeight);
SectionAnimation->setEndValue(collapsedHeight + contentHeight);
}
QPropertyAnimation* contentAnimation = static_cast<QPropertyAnimation *>(toggleAnimation-
>animationAt(toggleAnimation->animationCount() - 1));
contentAnimation->setDuration(animationDuration);
contentAnimation->setStartValue(0);
contentAnimation->setEndValue(contentHeight);
}
You have 2 widgets (Section and Section2) that contain other widgets (buttons/labels) that you want to be placed horizontally in your main window (inside centralWidget).
For that you need to set the appropriate layout to the centralWidget and add Section* items to that layout.
Right now, you just use the default layout of the centralWidget:
Section *section = new Section("Section", 100, ui->centralWidget);
ui->centralWidget->layout()->addWidget(section);
Section2 *section2 = new Section2("Section2", 100, ui->centralWidget);
ui->centralWidget->layout()->addWidget(section2);
I use QGridLayout to set the layouts to my elements, but I can't get results.
This is because in your code you use QGridLayout to lay out the widgets inside each instance of Section and Section2.
mainLayout->addWidget(toggleButton, 0, 0, 1, 1);
mainLayout->addWidget(headerLine, 0, 1, 1, 1);
mainLayout->addWidget(contentArea, 3, 0, 1, 3);
setLayout(mainLayout);
This controls how toggleButton, headerLine and contentArea are laid out inside Section* widgets, but has no effect on how Section widgets itself are positioned with respect to each other in some "higher level" widget (centralWidget in this case).
When you create your Section* widgets you can think of them as any other standard widget (button/label/etc.) and handle them accordingly.
I have a main window which opens a new window and connects a button from the new window to a "close" function. The problem arises when that new window has more than one button; it will always connect the last created button instead of the explicited one. Here is a sample working code:
"main.cpp"
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
"mainwindow.h"
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "screen_char_info.h"
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QPushButton *button_show_char_info;
Screen_Char_Info *screen_char_info;
QWidget *mainwidget;
QVBoxLayout *layout_main;
MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
button_show_char_info = new QPushButton("Character info", this);
layout_main = new QVBoxLayout();
mainwidget = new QWidget(this);
screen_char_info = NULL;
QObject::connect (button_show_char_info, &QPushButton::clicked, [this]{
if (screen_char_info == NULL) {
screen_char_info = new Screen_Char_Info();
screen_char_info->show();
QObject::connect (screen_char_info->button_return, &QPushButton::clicked, [=] {
screen_char_info->close();
screen_char_info = NULL;
});
}
});
layout_main->addWidget(button_show_char_info);
mainwidget->setLayout(layout_main);
setCentralWidget(mainwidget);
}
~MainWindow()
{
}
};
#endif // MAINWINDOW_H
"screen_char_info.h"
#ifndef SCREEN_CHAR_INFO_H
#define SCREEN_CHAR_INFO_H
#include <QString>
#include <QMenu>
#include <QMenuBar>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QWidget>
#include <QLineEdit>
#include <QGridLayout>
class Screen_Char_Info : public QWidget {
Q_OBJECT
public:
QPushButton *buttons_modify_attributes[15];
QPushButton *button_return;
QGridLayout *layout;
Screen_Char_Info () {
this->setAttribute(Qt::WA_DeleteOnClose);
this->setWindowTitle("Character Info");
layout = new QGridLayout(this);
for (int i = 0; i <= 15; i++) {
buttons_modify_attributes[i] = new QPushButton((i%2 ? "-" : "+"), this);
connect(buttons_modify_attributes[i], &QPushButton::clicked, [this] {
});
layout->addWidget(buttons_modify_attributes[i], (i / 2), (i % 2), 1, 1);
}
layout->addWidget(button_return = new QPushButton("Return", this), 8, 0, 1, 1);
this->setLayout(layout);
}
};
#endif // SCREEN_CHAR_INFO_H
However, if i put the line layout->addWidget(button_return... before the for loop, the button that closes the window is the last "-" button, and not the return one.
The way you do the connect does not appear to be conventional. Try using traditional Qt way:
connect(pButtonToPress, SIGNAL(clicked()), pObjectToHandle, SLOT(onClicked));
Provided QPushButton* pButtonPress actually pointing to QPushButton and pObjectToHandle to some object (can be 'this' pointer):
class ObjHandler
{
public slot:
void onClicked();
};
... should satisfy. SIGNAL and SLOT are macro that work with some help of Qt meta object compiler. That is why having slot: statement is highly critical.
Found the bug, I was declaring a button matrix with 15 elements, but iterating over a 16 element loop. The 16th element was the return button, and was overwritten in the loop.
I have the following minimal example code given.
main.cpp:
#include <QApplication>
#include "qt.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyDialog mainWin;
mainWin.show();
return app.exec();
}
qt.cpp:
#include <QLabel>
#include "qt.h"
void MyDialog::setupUi()
{
setCentralWidget(new QWidget);
mainLayout = new QVBoxLayout( centralWidget() );
centralWidget()->setLayout(mainLayout);
// show the add new effect channel button
QPushButton* newKnobBtn = new QPushButton("new", this );
connect( newKnobBtn, SIGNAL(clicked()), this, SLOT(addNewKnob()));
mainLayout->addWidget( newKnobBtn, 0, Qt::AlignRight );
containerWidget = new QWidget(this);
scrollArea = new QScrollArea(containerWidget);
mainLayout->addWidget(containerWidget);
scrollLayout = new QVBoxLayout(scrollArea);
scrollArea->setLayout(scrollLayout);
/*
QSizePolicy pol;
pol.setVerticalPolicy(QSizePolicy::Expanding);
setSizePolicy(pol);
*/
addNewKnob(); // to fit size initially
}
void MyDialog::addNewKnob()
{
scrollLayout->addWidget(new QLabel("Hello World", this));
/*
containerWidget->adjustSize();
adjustSize();
*/
}
qt.h:
#include <QMainWindow>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QPushButton>
class MyDialog : public QMainWindow
{
Q_OBJECT
private slots:
void addNewKnob();
private:
void setupUi();
QVBoxLayout* mainLayout;
QScrollArea* scrollArea;
QVBoxLayout* scrollLayout;
QWidget* containerWidget;
public:
MyDialog( ) { setupUi(); }
};
Compiling: Put all in one directory, type
qmake -project && qmake && make
I have the adjustSize() solution from here, but it does not work: (link). Everything I commented out was things I tried but did not help.
How do I make containerWidget and scrollLayout grow correctly, when a new Label is being added to scrollLayout?
Here's a simplified version that works for me:
qt.cpp:
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include "qt.h"
MyDialog::MyDialog()
{
QWidget * mainWidget = new QWidget;
QBoxLayout * mainLayout = new QVBoxLayout(mainWidget);
setCentralWidget(mainWidget);
// show the add new effect channel button
QPushButton* newKnobBtn = new QPushButton("new");
connect( newKnobBtn, SIGNAL(clicked()), this, SLOT(addNewKnob()));
mainLayout->addWidget( newKnobBtn, 0, Qt::AlignRight );
QScrollArea * scrollArea = new QScrollArea;
scrollArea->setWidgetResizable(true);
mainLayout->addWidget(scrollArea);
QWidget * labelsWidget = new QWidget;
labelsLayout = new QVBoxLayout(labelsWidget);
scrollArea->setWidget(labelsWidget);
addNewKnob(); // to fit size initially
}
void MyDialog::addNewKnob()
{
labelsLayout->addWidget(new QLabel("Hello World"));
}
qt.h:
#include <QMainWindow>
#include <QBoxLayout>
class MyDialog : public QMainWindow
{
Q_OBJECT
public:
MyDialog( );
private slots:
void addNewKnob();
private:
QBoxLayout * labelsLayout;
};
You have containerWidget that contain only one QScrollArea. I don't know why do you need this. But if you need this for some reason, you need to add a layout to this widget in order to make layouts work. Also do not create a layout for QScrollArea. It already have internally implemented layout. You should add scrollLayout to the scroll area's viewport() widget instead.
When you construct a layout and pass a widget to its constructor, the layout is automatically assigned to the passed widget. You should not call setLayout after that. This action will take no effect and produce console warning.
Alright so is there any way to make this program randomly change the variables x and y every time the button is clicked i am new to programming...
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtGUI>
#include <QWidget>
#include <cstdlib>
#include <ctime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
QPushButton *MainInter = new QPushButton("Push me!",window);
QPropertyAnimation *animation = new QPropertyAnimation(MainInter, "pos");
animation->setDuration(0);
animation->setEndValue(QPoint(x,y));
Object::connect(MainInter,SIGNAL(released()),animation,SLOT(start()));
window->resize(900,500);
window->show();
return a.exec();
}
What you can do is, instead of connecting the released() signal of your button directly to your animations start() SLOT, you would create your own custom SLOT. Then you connect the button to it, handle the action, and call the animation.
First read up on how to create a custom QWidget, instead of creating top level object in your main(). Simple example here
A custom widget might look like this:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QPushButton;
class QPropertyAnimation;
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
private:
QPushButton *button;
QPropertyAnimation *animation;
public slots:
void randomizeAnim();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QPushButton>
#include <QPropertyAnimation>
#include <ctime>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
button = new QPushButton("Push me!", this);
animation = new QPropertyAnimation(button, "pos");
animation->setDuration(0);
QObject::connect(button, SIGNAL(released()), this, SLOT(randomizeAnim()));
}
void MyWidget::randomizeAnim()
{
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
animation->setEndValue(QPoint(x,y));
animation->start();
}
And now your main.cpp can be reduced to the boilerplate code:
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new MyWidget;
window->resize(900,500);
window->show();
return a.exec();
}
Every time you click, your custom slot will handle the action and do the animation.