Qt Can't set centralWidget() properly - c++

I tried to build a simple GUI application. Then I got those warnings:
QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layoutQWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
I googled that you have to set central widget in MainWindow. And here is my implement which is still not working:
.h
class MainWindow : public QMainWindow
{
Q_OBJECT
QWidget *centralWidget;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void setButtons();
...
private:
Ui::MainWindow *ui;
QPushButton *btn[9][9];
}
.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
centralWidget = new QWidget(this);
this->setCentralWidget(centralWidget);
...
}
void MainWindow::setButtons()
{
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
btn[i][j] = new QPushButton(this);
...
QVBoxLayout *vLayout = new QVBoxLayout(this);
vLayout->addWidget(btn[i][j]);
centralWidget->setLayout(vLayout);
}
}
}
After trying this, I still received the warning messages, how can I solve this problem?
Thanks.

Your code has two basic issues. Firstly, the statement...
QVBoxLayout *vLayout = new QVBoxLayout(this);
will instantiate a new QVBoxLayout with this as its parent. Since this is of type MainWindow * and MainWindow inherits from QMainWindow you are effectively calling QMainWindow::setLayout -- that's the source of the error message...
QLayout: Attempting to add QLayout "" to MainWindow "", which already
has a layout
Secondly, you're creating a new QVBoxLayout on each loop iteration. If you really do want the buttons vertically aligned in a layout try something like...
void MainWindow::setButtons ()
{
QVBoxLayout *vLayout = new QVBoxLayout(centralWidget);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
btn[i][j] = new QPushButton;
...
vLayout->addWidget(btn[i][j]);
}
}
}

Related

Qt How can I get a signal from int when the value changes?

So lets say I a QDialog class like this
class DiagExample : public QDialog
{
Q_OBJECT
public:
DiagExample(QWidget *parent);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
};
And the implementation looks like this
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, [=](){myIntValue++;});
connect(MinusToValue, &QPushButton::released, [=](){myIntValue--;});
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(myIntValue, ???, [=](int newValue){
counter->setText(QString::number(newValue);});
}
I know that I could go straight from QPushButton::released -> setText on the QLabel, but my code will eventually have many inputs feeding into the counter, so from a point of readability and simplicity, I would rather have this sort of paradigm --- INPUT_WIDGET -> myIntValue -> setText(NEW VALUE OF myIntValue).
Standard way of doing such things in Qt:
class DiagExample : public QDialog
{
Q_OBJECT
Q_PROPERTY(intValue READ intValue NOTIFY onIntValueChange)
public:
DiagExample(QWidget *parent);
int intValue() const;
signals:
void onIntValueChange(int);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
};
Now in cpp:
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, [=](){
myIntValue++;
emit onIntValueChange(myIntValue);
});
connect(MinusToValue, &QPushButton::released, [=](){
myIntValue--;
emit onIntValueChange(myIntValue);
});
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(this, &DiagExample::onIntValueChange, [=](int newValue){
counter->setText(QString::number(newValue);});
}
int DiagExample::intValue() const {
return myIntValue;
}
Note only QObject can emit signals!
You will have to define it yourself, something like that could work:
class DiagExample : public QDialog
{
Q_OBJECT
public:
DiagExample(QWidget *parent);
private:
int myIntValue = 0;
QPushButton *AddToValue;
QPushButton *MinusToValue;
QLabel *counter;
private slots:
void onPlusOne();
void onMinusOne();
signals:
void intValueChanged(const QString& newVal);
};
and then in you cpp file:
DiagExample::DiagExample(QWidget *parent) : QDialog(parent)
{
setWindowTitle("Example Diag");
QVBoxLayout *layout = new QVBoxLayout(this);
AddToValue = new QPushButton(tr("Add"));
MinusToValue = new QPushButton(tr("Minus"));
counter = new QLabel;
connect(AddToValue, &QPushButton::released, &DiagExample::onPlusOne);
connect(MinusToValue, &QPushButton::released, &DiagExample::onMinusOne);
connect(this, &DiagExample::intValueChanged, counter, &QLabel::setText);
layout->addWidget(AddToValue);
layout->addWidget(MinusToValue);
layout->addWidget(counter);
// below is what I want to achieve but have no idea how
// essentially when value of the int is changed, text of
// counter (QLabel) will be set to the new value
connect(myIntValue, ???, [=](int newValue){
counter->setText(QString::number(newValue);});
}
void DiagExample::onPlusOne()
{
myIntValue++;
emit intValueChanged(QString::number(myIntValue));
}
void DiagExample::onMinusOne()
{
myIntValue--;
emit intValueChanged(QString::number(myIntValue));
}

How to replace QPushButton with QLabel in CentralWidget in Qt?

I am trying to learn and build a small minesweeper gui app.Here is how this looks like:
The next thing I want to do is after clicking one button, then the button will be set to hide() and a QLabel will appear in the same place.
My code is like this:
.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void setUI();
void clickedBtnInfo();
private:
Ui::MainWindow *ui;
QWidget *centralWidget;
QGridLayout *centralLayout;
QPushButton *btn[81];
QPushButton *btnSender;
QLabel *lbl[81];
QString clickedBtnName;
private slots:
void btnClicked();
};
.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
lbl[81] = new QLabel(centralWidget);
setUI();
for(int i = 0; i < 81; i++) {
connect(btn[i], SIGNAL(clicked(bool)), btn[i], SLOT(hide()));
connect(btn[i], SIGNAL(clicked(bool)), this, SLOT(btnClicked()));
}
centralWidget->setLayout(centralLayout);
}
void MainWindow::setUI()
{
...
centralLayout = new QGridLayout(centralWidget);
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
centralLayout->addWidget(btn[j + i * 9], 0 + i, j);
centralLayout->setSpacing(0);
}
}
...
}
void MainWindow::clickedBtnInfo()
{
btnSender = qobject_cast<QPushButton*>(sender());
clickedBtnName = btnSender->objectName();
}
void MainWindow::btnClicked()
{
clickedBtnInfo();
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
if(btn[j + i * 9]->objectName() == clickedBtnName) {
centralLayout->addWidget(lbl[j + i * 9], 0 + i, j);
centralLayout->setSpacing(0);
}
}
}
}
When I ran this and clicked one of the buttons, the app just force quit(The program has unexpectedly finished.)
So how can I solve this problem and replace QPushButton with QLabel after clicking? Thanks.
The Problem why your code leads to a crash was correctly pointed out by #G.M. - lbl[81] = new QLabel(centralWidget); will create only 1 label, and place it in the 81st array field. That are 2 errors at once:
If your array is 81 elements long, they are numbered: 0, 1, ..., 79, 80. The last element is 80, because you start counting at 0. So placing something at position 81 is not possible
To actually create 81 new labels, you have to create them in a loop:
Sample code:
for(int i = 0; i < 81; i++) { //goes from 0 to 80
lbl[i] = new QLabel(centralWidget);
lbl[i]->setObjectName(QStringLiteral("Label %1").arg(i));
}
The second line gives each label a custom name. See QString::arg for details.
One more tip: Avoid C-arrays, unless you need high performace/low memory (which is not the case for your example). Instead try to use one of the Qt container classes, e.g. QList or QVector. (You can use std::vector etc. as well, but when working with Qt, I would recommend to use the Qt containers)
For your case, I would recommend QVector, as it performs best with fixed-sized arrays. With both of these changes, update your code to:
class MainWindow : public QMainWindow
{
//...
private:
Ui::MainWindow *ui;
QWidget *centralWidget;
QGridLayout *centralLayout;
QVector<QPushButton> btn;
QPushButton *btnSender;
QVector<QLabel> lbl;
QString clickedBtnName;
};
In your cpp file, update the part where you create the arrays:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
lbl.resize(81);
for(int i = 0; i < lbl.size(); i++) { //goes from 0 to 80
lbl[i] = new QLabel(centralWidget);
lbl[i]->setObjectName(QStringLiteral("Label %1").arg(i));
}
setUI();
//...
}
void MainWindow::setUI()
{
//keep your code, but remember to prepare the btn vector with:
btn.resize(81);
//then you can fill the vector just like you are used to:
btn[0] = ui->btn0;
//...
}
And the rest stays the same, as these classes allow you to keep the standard array access syntax you know.

How to show the row where QPushButton is clicked in QTableWidget

I would like to delete row where QPushButton is clicked how it is possible to I think it is reasonable to use slots but how to do it don't know , if you have any ideas how to get a row of selected button please share, thanks.
It is my table
It is a code where i add rows to my QTableWidget
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int i = 0; i<20;i++)
ui->tableWidget->insertRow(ui->tableWidget->rowCount());
QVector<QString>vec;
vec<<"first"<<"sec"<<"third"<<"for"<<"fif"<<"first"<<"sec"
<<"third"<<"for"<<"fif";
vec<<"first"<<"sec"<<"third"<<"for"<<"fif"<<"first"<<"sec"
<<"third"<<"for"<<"fif";
for(int i = 0; i<ui->tableWidget->rowCount();i++)
{
for(int j = 0; j<ui->tableWidget->columnCount();j++)
{
if(j == 0)
{
QWidget* pWidget = new QWidget();
QPushButton* btn_edit = new QPushButton();
btn_edit->setText("Remove");
QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(btn_edit);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0, 0, 0, 0);
pWidget->setLayout(pLayout);
ui->tableWidget->setCellWidget(i, j, pWidget);
continue;
}
QTableWidgetItem*item = new QTableWidgetItem(vec[i]);
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
ui->tableWidget->setItem(i, j, item);
}
}
}
This task can be solved using the removeRow() method but before we must get the row. First of all we will connect all the buttons to a slot inside the loop as shown below:
*.h
private slots:
void onClicked();
*.cpp
[...]
QPushButton* btn_edit = new QPushButton();
btn_edit->setText("Remove");
connect(btn_edit, &QPushButton::clicked, this, &MainWindow::onClicked);
[...]
In the slot we can get the button that emits the signal through the sender() method, then we get QWidget parent (created with the name pWidget), this is the widget that is added to the QTableWidget and its position is relative to it, then we use the method indexAt() to get the QModelIndex associated with the cell, and this has the information of the row through the method row(). All of the above is implemented in the following lines:
void MainWindow::onClicked()
{
QWidget *w = qobject_cast<QWidget *>(sender()->parent());
if(w){
int row = ui->tableWidget->indexAt(w->pos()).row();
ui->tableWidget->removeRow(row);
ui->tableWidget->setCurrentCell(0, 0);
}
}
Note: The setCurrentCell() method is used to set the focus since the last cell that has it has been deleted.
The complete example can be found in the following link.
When you are creating QPushButton just add :
btn_delete = new QPushButton("Remove", ui->tableWidget);
btn_delete->setObjectName(QString("%1").arg(ui->tableWidget->rowCount()));
connect(btn_delete, SIGNAL(clicked()), this, SLOT(CellButtonDeleteClicked()));
And create function CellButtonDeleteClicked()
void CellButtonDeleteClicked()
{
// by this line I can get the sender of signal
QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
int row = pb->objectName().toInt();
ui->tableWidget->removeRow(row);
}

Qt, crash when QStackedWidget's button try to access to another QStackedWidget's label

Main creates two QStackedWidget, class Body and class Bottom.
In class Body there is a button and when it is pressed it should change the text in the label in Bottom. The program crash when Body->button try to use the public function of Bottom which change the private label.
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//GRAPHIC INTERFACE
QStackedWidget *stackedWidget_body = new QStackedWidget;
QStackedWidget *stackedWidget_bottom = new QStackedWidget;
//create classe
Body * wBody ;
Bottom *wBottom;
wBody = new Body(wBottom); //pass the reference of the Bottom class, wBottom
wBottom = new Bottom();
//wBottom->setLLabel(); //this command will change the name of the label, it means that setLLabel works well
//insert the body widget
stackedWidget_body->addWidget(wBody);
//insert the bottom widget
stackedWidget_bottom->addWidget(wBottom);
//layout
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(stackedWidget_body);
layout->addWidget(stackedWidget_bottom);
//WINDOW
QWidget window;
window.setLayout(layout);
window.show();
a.exec();
return 0 ;
}
Body.h
namespace Ui {
class Body;
}
class Body : public QWidget
{
Q_OBJECT
public:
explicit Body(Bottom *_wBottom, QWidget *parent = 0);
~Body();
public slots:
void test(); // it will be called then the button will be pressed
private:
Ui::Body *ui;
Bottom *wBottom;
void testSend(Bottom *wBottom);
};
Body.cpp
Body::Body(Bottom *_wBottom, QWidget *parent) :
wBottom(_wBottom),
QWidget(parent),
ui(new Ui::Body)
{
QPushButton *button_standard2 = new QPushButton("test");
connect(button_standard2, SIGNAL(clicked(bool)),this, SLOT(test()));
//layout
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button_standard2);
this->setLayout(layout);
ui->setupUi(this);
}
Body::~Body()
{delete ui;}
void Body::test(){
testSend(wBottom);
}
void Body::testSend(Bottom *wBottomX)
{
wBottomX->setLLabel();
}
bottom.h
namespace Ui {
class Bottom;
}
class Bottom : public QWidget
{
Q_OBJECT
public:
explicit Bottom(QWidget *parent = 0);
~Bottom();
void setLLabel();
private:
Ui::Bottom *ui;
QLabel *bLabel;
};
bottom.cpp
Bottom::Bottom(QWidget *parent) :
QWidget(parent),
ui(new Ui::Bottom)
{
bLabel = new QLabel("Name");
//layout
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(bLabel);
//setLLabel(); //this functions does it work
this->setLayout(layout);
ui->setupUi(this);
}
Bottom::~Bottom()
{delete ui;}
void Bottom::setLLabel(){
std::cout<<"change";
Bottom::bLabel->setText("value"); //The problem is here, when the function is called from Body::testSend
}
Can you give me any tip?
//create classe
Body * wBody ;
Bottom *wBottom;
wBody = new Body(wBottom); //pass the reference of the Bottom class, wBottom
wBottom = new Bottom();
You are sending an invalid wBottom instance to wBody. Try interchanging the last two lines.

How to update a QTreeWidget by Clicking on QPushbutton

I'm developing a Qt app in C++
I have create a layout with button bar, TreeView...
The Treeview is defined in my MainWindow.cpp
MainWindow::MainWindow()
{
setWindowTitle(QString::fromUtf8("PULS"));
resize(800,600);
setUnifiedTitleAndToolBarOnMac(true);
createDeviceStatusBar();
createSectionBar();
createTreeView();
QWidget *MainWindowWidget = new QWidget();
QVBoxLayout *MainWindowLayout = new QVBoxLayout(MainWindowWidget);
BrowserSection = new QGroupBox();
QWidget *BrowserWidget = new QWidget();
QHBoxLayout *BrowserLayout = new QHBoxLayout(BrowserWidget);
BrowserLayout->addWidget(SectionBar);
BrowserLayout->addWidget(TreeSection);
BrowserSection->setLayout(BrowserLayout);
MainWindowLayout->addWidget(DeviceSection);
MainWindowLayout->addWidget(BrowserSection);
setCentralWidget(MainWindowWidget);
show();
}
The createSectionBar() is a layout defined as below:
void MainWindow::createSectionBar()
{
SectionBar = new QGroupBox();
QVBoxLayout *SectionBarlayout = new QVBoxLayout;
SectionBarlayout->setContentsMargins(QMargins(0,0,0,0));
MusicButton = new QPushButton();
MusicButton->setFixedSize(110,150);
MusicButton->setIcon(QIcon(":/images/music.png"));
MusicButton->setIconSize(QSize(90,144));
MusicButton->setFlat(true);
MusicButton->setAutoFillBackground(true);
connect(MusicButton, SIGNAL(clicked()), this, SLOT(MusicTreeFile()));
SectionBarlayout->addWidget(MusicButton);
SectionBar->setLayout(SectionBarlayout);
}
The createTreeView() is defined as below to enable TreeView and Items.
void MainWindow::createTreeView()
{
TreeSection = new QGroupBox();
QVBoxLayout *TreeLayout = new QVBoxLayout;
MyTree = new TreeWidget();
MyTree->setSortingEnabled(true);
MyTree->setColumnWidth(0, 400);
QTreeWidgetItem* headerItem = new QTreeWidgetItem();
headerItem->setText(0,QString("File Name"));
headerItem->setText(1,QString("Size (Bytes)"));
headerItem->setText(2,QString("Date"));
MyTree->setHeaderItem(headerItem);
MyTree->setAutoFillBackground(true);
TreeLayout->addWidget(MyTree);
TreeSection->setLayout(TreeLayout);
}
What I need is to find a way to file the MyTree with
while (file != NULL && file->parent_id == folder_parent) {
QTreeWidgetItem* item = new QTreeWidgetItem();
int i;
LIBMTP_file_t *oldfile;
item->setText(0,file->filename);
Tree->addTopLevelItem(item);
....
}
here is the header file:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
protected:
private slots:
void MusicTreeFile();
private:
void createSectionBar();
void createTreeView();
void createDeviceStatusBar();
QGroupBox *SectionBar;
QGroupBox *TreeSection;
QGroupBox *DeviceSection;
QGroupBox *BrowserSection;
QPushButton *MusicButton;
TreeWidget *MyTree;
};
but it needs to be done only when clicking on MusicPushButton, I have already connected the MusicTreeFile() to the PushButton() action. but how to access to MyTree as it's also defined in another class..
You do not connect a signal to a class. You connect it to an instance of a class. And both of your instances are defined in MainWindow. So after both widgets are created you can call
QObject::connect(this->MusicButton, SIGNAL(clicked()), this->MyTree, SLOT(slot_name()));
With slot_name being your function
TreeWidget::slot_name{
while (file != NULL && file->parent_id == folder_parent) {
....
}
}