So I tired to redo my code but either I get QLayout: Cannot add null widget to QGridLayout/cardLayout or the program crashes.
From the beginning. I created a new class StationCard. This class is based on the QGroupBox and adds a QLabel to it, right?
.h
#ifndef STATIONCARD_H
#define STATIONCARD_H
#include <QLabel>
#include <QGroupBox>
#include <QLayout>
public:
StationCard();
private:
};
#endif // STATIONCARD_H
#ifndef STATIONCARD_H
#define STATIONCARD_H
#include <QLabel>
#include <QGroupBox>
#include <QLayout>
class StationCard : public QGroupBox{
public:
StationCard();
QLabel *propLabel;
private:
};
#endif // STATIONCARD_H
To copy to clipboard, switch view to plain text mode
.cpp
#include "stationcard.h"
StationCard::StationCard(){
layout->addWidget(propLabel, 10, Qt::AlignRight);
setLayout(layout);
}
#include "stationcard.h"
StationCard::StationCard(){
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(propLabel, 10, Qt::AlignRight);
setLayout(layout);
}
To copy to clipboard, switch view to plain text mode
In my main window .h I added the .h file of the new class and crated a vector:
QVector<StationCard*> stationVector;
QVector<StationCard*> stationVector;
To copy to clipboard, switch view to plain text mode
In my main window .cpp I first want to fill my grid with empty cards:
stationVector.resize(8);
int k = 0;
for(int i = 0; i < 2; ++i){
ui->cardLayout->setRowMinimumHeight(i,200);
for(int j = 0; j < 4; ++j){
ui->cardLayout->setColumnMinimumWidth(j, 150);
ui->cardLayout->addWidget(stationVector[k], i, j);
k++;
}
}
stationVector.resize(8);
int k = 0;
for(int i = 0; i < 2; ++i){
ui->cardLayout->setRowMinimumHeight(i,200);
for(int j = 0; j < 4; ++j){
ui->cardLayout->setColumnMinimumWidth(j, 150);
ui->cardLayout->addWidget(stationVector[k], i, j);
k++;
}
}
To copy to clipboard, switch view to plain text mode
But this already gives me QLayout: Cannot add null widget to QGridLayout/cardLayout and if I want to fill it, the program crashes. If I say stationVector.resize(8), does this not create an array with eight elements of my custom group box class? If I try to do stationVector[k] = new StationCard, it crashes right away. =(
Added after 5 minutes:
Originally Posted by
anda_skoa
But your code had stationCard (and previously propLabel) as a container, it is accessed by index in your code snippets.
_
Actually no. I followed a tutorial here where they created several buttons using a for-loop. http://doc.qt.digia.com/4.4/layouts-basiclayouts.html
Bookmarks