I have a class StackedWidget which inherits from QStackedWidget. The standard behaviour of QStackedWidget is that it adopts the size of its biggest element. What I want to do is force it to resize to its current element. I have already tried a couple of solutions found here and none of them work including e.g. setting size policies or calling hide() on all the widgets that go out of sight.
I think problem may reside in two possibilities:
1. Something is terribly wrong with StackedWidget. 2. The problem of resizing does not pertain directly to StackedWidget but to some layout or another widget that StackedWidget is nested in.
Concerning the first possibility, here I provide the StackedWidget class.
Please take a look at what my StackedWidget's declaration looks like:
class StackedWidget : public QStackedWidget
{
Q_OBJECT
private:
QComboBox* widgetChooser = nullptr;
public:
StackedWidget(QWidget* parent) : QStackedWidget(parent) {}
void setWidgetChooser(QComboBox* widgetChooser);
void addWidget(QWidget* widget);
private:
void makeConnection();
signals:
public slots:
void setCurrentIndex(const int& index);
};
Here goes the definition:
void StackedWidget::setWidgetChooser(QComboBox* widgetChooser)
{
if (widgetChooser == nullptr) throw RuntimeError("widgetChooser is nullptr");
this->widgetChooser = widgetChooser;
makeConnection();
}
void StackedWidget::addWidget(QWidget* widget)
{
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QStackedWidget::addWidget(widget);
}
void StackedWidget::makeConnection()
{
connect(widgetChooser, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentIndex(int)));
}
void StackedWidget::setCurrentIndex(const int& index)
{
qDebug() << "Index changed to " << index;
QWidget* pWidget = widget(index);
Q_ASSERT(pWidget);
QStackedWidget::setCurrentWidget(pWidget);
pWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pWidget->adjustSize();
adjustSize();
}
Concering the second possibility, here goes how I use StackedWidget.
void SchemaWidget::TempBuildAlt(const SchemaElement* son, QVBoxLayout* parentLayout)
{
if (auto schemaAlternative = dynamic_cast<const SchemaAlternatives*>(son))
{
QComboBox* widgetBox = BuildWidgetChooserBox(schemaAlternative);
StackedWidget* stackedWidget = BuildStackedWidget(schemaAlternative);
stackedWidget->setWidgetChooser(widgetBox);
QHBoxLayout* boxLayout = new QHBoxLayout;
boxLayout->addWidget(new QLabel(schemaAlternative->Name, this));
boxLayout->addWidget(widgetBox);
QVBoxLayout* layout = new QVBoxLayout;
layout->addLayout(boxLayout);
layout->addWidget(stackedWidget);
parentLayout->addLayout(layout);
} else throw RuntimeError("Cannot cast son to SchemaAlternatives.");
}
QComboBox* SchemaWidget::BuildWidgetChooserBox(const SchemaAlternatives* schema)
{
QComboBox* alternativesBox = new QComboBox(this);
QStringListModel* listModel = new QStringListModel(this);
QStringList list;
for (int i = 1; i <= schema->Sons.High(); ++i)
{
if (const SchemaTree* son = dynamic_cast<const SchemaTree*>(schema->Sons(i).Get()))
{
list << son->Name;
}
else throw RuntimeError("Son cannot be cast to SchemaTree.");
}
listModel->setStringList(list);
alternativesBox->setModel(listModel);
return alternativesBox;
}
StackedWidget* SchemaWidget::BuildStackedWidget(const SchemaAlternatives* schema)
{
StackedWidget* stackedWidget = new StackedWidget(this);
for (int i = 1; i <= schema->Sons.High(); ++i)
{
if (const SchemaTree* alternativeSon = dynamic_cast<const SchemaTree*>(schema->Sons(i).Get()))
{
QWidget* widget = new QWidget(this);
QVBoxLayout* widgetLayout = new QVBoxLayout;
BuildWidget(alternativeSon, widgetLayout);
widget->setLayout(widgetLayout);
stackedWidget->addWidget(widget);
}
else throw RuntimeError("Son could not be cast to SchemaTree.");
}
return stackedWidget;
}
I want to say thanks in advance to anyone who will be willing to spend some time on my problem. I do appreciate it. Thanks. You're great.
Related
On a subclassed QListWidget I have several items. Every QListWidget item (e.g. "ROS Init", "Images" etc) that is shown below is associated with a specific icon.
The problem I have is that I am trying to drag and drop the specific icon corresponding to that QListWidget item, but nothing happens.
Below the function responsible for the dragging:
void ListView::startDrag(Qt::DropActions supportedActions)
{
QMap<int, QString> icons;
icons.insert(IT_RosInit, "ROS Init");
icons.insert(IT_Images, "Images");
icons.insert(IT_Path, "Path");
icons.insert(IT_RosShutDown, "ROS Shutdown");
if (supportedActions & Qt::CopyAction)
{
const QList<QListWidgetItem *> &m_items(selectedItems());
if (m_items.isEmpty())
return;
QPixmap pixmapLaser("/home/images/laserscan.png");
QPixmap pixmapPCloud2("/home/images/pcloud2.png");
// etc ...
QStringList iconImages;
for(int i = 0; i < icons.count(); ++i)
{
for (const QString &tableType : iconImages) {
if (tableType == "ROS Init")
{
auto *data = mimeData(m_items);
auto *drag = new QDrag(this);
drag->setPixmap(pixmapLaser);
drag->setMimeData(data);
drag->setHotSpot(pixmapLaser.rect().center());
drag->exec(Qt::CopyAction);
}
else if(tableType == "Images")
{
auto *data2 = mimeData(m_items);
auto *drag2 = new QDrag(this);
drag2->setPixmap(pixmapPCloud2);
drag2->setMimeData(data2);
drag2->setHotSpot(pixmapPCloud2.rect().center());
drag2->exec(Qt::CopyAction);
}
}
}
}
else
{
QListWidget::startDrag(supportedActions);
}
}
After subclassing the QListWidget I just reimplemented the usual drag and drop function. All other function are working properly but the startDrag and in fact as I try to drag the proper QPixmap, I actually see nothing being dragged.
I consulted this source, useful, and also this other source which was useful but it didn't reimplement the startDrag but instead dropEvent which for me is not a problem because that is working well.
I also consulted this source and this other source but that also didn't help fixing the problem.
Thanks for shedding light on this matter for solving the problem
Solution
I would approach this problem in the following way:
Set the ItemIsDragEnabled flag of QListWidgetItem to enable the item for dragging:
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
Set the desired data for each item:
item->setData(Qt::UserRole, type);
where type is one of the enumerated values IT_RosInit, IT_Images, etc.
Enable the drag functionality of the ListWidget:
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DragOnly);
Use the item settings to setup the QDrag object, created in startDrag.
Example
Here is an example I have prepared for you to demonstrate how the proposed solution could be implemented:
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
auto *l = new QVBoxLayout(this);
auto *list = new ListView(this);
list->addItem(createItem(":/pix/images/laserscan.png", tr("RosInit"), IT_RosInit));
list->addItem(createItem(":/pix/images/icons/pcloud2.png", tr("Images"), IT_Images));
list->addItem(createItem(":/pix/images/icons/some_icon.png", tr("Path"), IT_Path));
list->addItem(createItem(":/pix/images/icons/another_icon.png", tr("RosShutDown"), IT_RosShutDown));
l->addWidget(list);
resize(300, 400);
setWindowTitle("IconDrag");
}
QListWidgetItem *MainWindow::createItem(const QString &pm, const QString &text, int type)
{
auto *item = new QListWidgetItem(QIcon(QPixmap(pm)), text);
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
item->setData(Qt::UserRole, type);
return item;
}
ListView.cpp
ListView::ListView(QWidget *parent) :
QListWidget(parent)
{
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DragOnly);
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::SingleSelection);
}
void ListView::startDrag(Qt::DropActions supportedActions)
{
if (supportedActions & Qt::CopyAction) {
const QList<QListWidgetItem *> &items(selectedItems());
if (items.isEmpty())
return;
const QPixmap &pm(items.first()->icon().pixmap(64));
auto *item = items.first();
auto *mimeData = new QMimeData();
auto *drag = new QDrag(this);
mimeData->setData("text/plain", item->data(Qt::UserRole).toByteArray());
drag->setPixmap(pm);
drag->setMimeData(mimeData);
drag->setHotSpot(pm.rect().center());
drag->exec(Qt::CopyAction);
}
}
I would like to get a number of a QTableWidget row after selecting some topic in comboBox how it is possible to get the row, thanks.
void MainWindow::metto_stringa(int i)
{
QWidget *w = qobject_cast<QWidget *>(sender()->parent());
if(w)
{
int row = ui->tableWidget->indexAt(w->pos()).row();
ui->lineEdit->setText(QString::number( row ));
}
// ui->lineEdit->setText(QString::number( i ));
}
else if(i == 3)
{
// ui->tableWidget->setCellWidget(ui->tableWidget->rowCount(), i, "");
QString s = "Normal";
QComboBox *combo = new QComboBox;
combo->addItem("Below normal");
combo->addItem("Normal");
combo->addItem("Above normal");
combo->addItem("High");
combo->addItem("Real time");
connect(combo,SIGNAL(currentIndexChanged(int)),this,
SLOT(metto_stringa(int)));
ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1, i,combo);
/* ui->tableWidget->setCellWidget(i,4,combo);
QTableWidgetItem*item = new QTableWidgetItem(s);
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, i,
item);*/
continue;
}
In this case you should not use the parent of the QComboBox, you must use the same sender()
void MainWindow::metto_stringa(int index)
{
QWidget *w = qobject_cast<QWidget *>(sender());
if(w)
{
int row = ui->tableWidget->indexAt(w->pos()).row();
ui->lineEdit->setText(QString::number(row));
}
}
In the question I answered before I commented that you must access the widget that you use in the setCellWidget() function, in the previous case the widget had the following form:
QWidget <--- QPushButton
parent() sender()
ie you owe to that widget so we take advantage of sender() and parent() in the previous case. In the current case QComboBox is added directly.
I want to create a scrollable widget which contains a collection of child widgets managed by QBoxLayout, and it must be easy to add and remove widgets from this collection. But when I add child widgets to it, viewport widget did not expand its size (remain initial size), instead children are overlapping on themselves. I do not know what to do to fix this.
Here is the code:
mainwidget.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QtGui/QScrollArea>
#include <QResizeEvent>
#include <QDebug>
class CMainWidget : public QScrollArea
{
Q_OBJECT
public:
CMainWidget(QWidget *parent = 0);
~CMainWidget();
protected:
virtual void resizeEvent(QResizeEvent *pEvent);
virtual void keyPressEvent(QKeyEvent *pEvent);
};
#endif // MAINWIDGET_H
mainwidget.cpp
#include "mainwidget.h"
#include "rootitem.h"
CMainWidget::CMainWidget(QWidget *parent)
: QScrollArea(parent)
{
QWidget* pViewport = new QWidget();
QBoxLayout* pLayout = new QBoxLayout(QBoxLayout::TopToBottom);
pLayout->setSizeConstraint(QLayout::SetNoConstraint);
for (int iWidgetIndex = 0; iWidgetIndex < 20; iWidgetIndex++)
pLayout->addWidget(new CRootItem());
pViewport->setLayout(pLayout);
pViewport->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
setWidget(pViewport);
}
CMainWidget::~CMainWidget()
{
}
void CMainWidget::resizeEvent(QResizeEvent *pEvent)
{
QWidget* pViewport = widget();
int iHeight = pViewport->height();
int iWidth = pEvent->size().width();
pViewport->resize(iWidth, iHeight);
}
void CMainWidget::keyPressEvent(QKeyEvent *pEvent)
{
QWidget* pViewport = widget();
QBoxLayout* pLayout = (QBoxLayout*)pViewport->layout();
if (pEvent->key() == Qt::Key_Space)
{
pLayout->addWidget(new CRootItem());
qDebug() << "adding...";
}
if (pEvent->key() == Qt::Key_C)
{
if (!pLayout->isEmpty())
{
QLayoutItem* pItem = pLayout->itemAt(0);
pLayout->removeItem(pItem);
delete pItem->widget();
delete pItem;
qDebug() << "removing...";
}
}
}
Here is the image which shows child widgets overlapping after inserting a couple of new items:
EDIT
Just solved my problem using dirty approach: subtraction and adding item fixed height from viewport's. Is there more fashion way to handle this problem?
Code:
void CMainWidget::keyPressEvent(QKeyEvent *pEvent)
{
QWidget* pViewport = widget();
QBoxLayout* pLayout = (QBoxLayout*)pViewport->layout();
if (pEvent->key() == Qt::Key_Space)
{
QWidget* pItem = new CRootItem();
pLayout->addWidget(pItem);
QSize Size = pViewport->size();
Size.rheight() += pItem->height() + pLayout->spacing();
pViewport->resize(Size);
qDebug() << "adding...";
}
if (pEvent->key() == Qt::Key_C)
{
if (!pLayout->isEmpty())
{
QLayoutItem* pItem = pLayout->itemAt(0);
pLayout->removeItem(pItem);
QSize Size = pViewport->size();
Size.rheight() -= pItem->widget()->height() + pLayout->spacing();
pViewport->resize(Size);
delete pItem->widget();
delete pItem;
qDebug() << "removing...";
}
}
}
Comment out your resizeEvent. You rarely need to set the explicit size or position of a widget unless it's a top level window. Let the layout do the work.
I have
QWidget *myWidget; //on my mainWindow with Qt Creator
and a
QComboBox *myComboBox;
When I change my selection of myComboBox, I would like to delete
myWidget->layout();
and set to myWidget another layout.
I'm doing : (with this help : https://stackoverflow.com/a/12034868/6105710)
void ConfWindow::on_comboBouton_currentIndexChanged(const QString &arg1)
{
if(ui->myWidget->layout()){
auto myLayout = ui->myWidget->layout();
QLayoutItem *item;
while((item = myLayout->takeAt(0)) != 0){
myLayout->removeItem(item);
delete item;
}
delete myLayout;
}
for(int i=0; i< someVector->size(); i++){
if( someVector->at(i)->getName() == arg1){
ui->myWidget->setLayout( someVector->at(i)->getLayout()); //which return a QHBoxLayout.
break;
}
}
}
and when I change my comboBox, It is painting my new layout, but my old layout still here..
I tried repaint(), update() etc...
Is it because I create layout in another class and add it with
auto myVector = new QVector<myClass>;
setLayout( myVector->getLayout() )
? I don't think so because I'm using pointer .. I don't understand ...
I was wondering if anybody could tell me whats actually going on here. I have included the header and implementation file for a simple config dialog. The problem is in the updateAutoSaveGroupBox slot I cannot access or change any properties of my widgets on the page. I.E. I want to make some widgets disabled if a check box has not been checked but when I try to set them I get a read access violation. Any help with this matter would be greatly appreciated. The problems exsist in these two lines(commented out so it will run for now without throwing an exception).
//autoSaveLabel->setDisabled(autoSaveIsEnabled);
//autoSaveSpinBox->setDisabled(getAutoSaveIsEnabled());
configWidget.h
class EnigmaConfigGeneralEnvironmentWidget : public QWidget
{
Q_OBJECT
public:
explicit EnigmaConfigGeneralEnvironmentWidget(QWidget *parent = 0);
~EnigmaConfigGeneralEnvironmentWidget();
signals:
void setAutoSaveIsEnabledSignal(bool autoSaveIsEnabled);
public slots:
void setAutoSaveIsEnabled(bool autoSaveIsEnabled){_AutoSaveIsenabled = autoSaveIsEnabled;}
bool getAutoSaveIsEnabled(){return _AutoSaveIsenabled;}
void updateAutoSaveGroupBox(bool autoSaveIsEnabled);
private:
void makeConnections();
void readSettings();
void writeSettings();
void createMainWidget();
QGroupBox *uiGroupBox;
QStringList localList;
QLabel *localLabel;
QComboBox *localeComboBox;
QHBoxLayout *localSelectionHLayout;
QGroupBox *systemGroupBox;
QCheckBox *autoSaveCheckBox;
QLabel *autoSaveLabel;
QSpinBox *autoSaveSpinBox;
QHBoxLayout *autoSaveHLayout;
bool _AutoSaveIsenabled;
};
ConfigWidget.cpp
#include "enigmaconfiggeneralenvironmentwidget.h"
#include <QtWidgets>
EnigmaConfigGeneralEnvironmentWidget::EnigmaConfigGeneralEnvironmentWidget(QWidget *parent) :
QWidget(parent)
{
makeConnections();
readSettings();
createMainWidget();
}
EnigmaConfigGeneralEnvironmentWidget::~EnigmaConfigGeneralEnvironmentWidget()
{
writeSettings();
}
void EnigmaConfigGeneralEnvironmentWidget::makeConnections()
{connect(this,SIGNAL(setAutoSaveIsEnabledSignal(bool)),this,SLOT(setAutoSaveIsEnabled(bool)) );
connect(this,SIGNAL(setAutoSaveIsEnabledSignal(bool)),this,SLOT(updateAutoSaveGroupBox(bool)) );
}
void EnigmaConfigGeneralEnvironmentWidget::readSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
bool autoSaveIsEnabled = settings.value("autoSaveIsEnabled",bool(true)).toBool();
setAutoSaveIsEnabledSignal(autoSaveIsEnabled);
settings.endGroup();
}
void EnigmaConfigGeneralEnvironmentWidget::writeSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
settings.setValue("autoSaveIsEnabled",getAutoSaveIsEnabled());
settings.endGroup();
}
void EnigmaConfigGeneralEnvironmentWidget::createMainWidget()
{
localList.append(tr("Danish - ???"));
localList.append(tr("English - Australia"));
localList.append(tr("English - Canada"));
localList.append(tr("English - USA"));
localList.append(tr("English - UK"));
localList.append(tr("Finnish - Finland"));
localList.append(tr("French - Canada"));
localList.append(tr("French - France"));
localList.append(tr("Norwegian - ???"));
localList.append(tr("Swedish - ???"));
uiGroupBox = new QGroupBox();
uiGroupBox->setTitle(tr("UI Settings"));
localLabel= new QLabel();
localLabel->setText(tr("Select a language: "));
localeComboBox = new QComboBox();
localeComboBox->addItems(localList);
localSelectionHLayout = new QHBoxLayout(uiGroupBox);
localSelectionHLayout->addWidget(localLabel);
localSelectionHLayout->addWidget(localeComboBox);
systemGroupBox = new QGroupBox();
systemGroupBox->setTitle(tr("System Settigns"));
autoSaveCheckBox = new QCheckBox();
autoSaveCheckBox->setText(tr("Auto-Save Enabled: "));
autoSaveCheckBox->setChecked(getAutoSaveIsEnabled());
connect(autoSaveCheckBox,SIGNAL(clicked(bool)),this,SIGNAL(setAutoSaveIsEnabledSignal(bool)));
autoSaveLabel = new QLabel(this);
autoSaveLabel->setText(tr("Auto-Save Interval is Every: "));
autoSaveSpinBox = new QSpinBox();
autoSaveSpinBox->setSuffix(tr("Mins."));
autoSaveSpinBox->setAccelerated(true);
autoSaveHLayout = new QHBoxLayout(systemGroupBox);
autoSaveHLayout->addWidget(autoSaveCheckBox);
autoSaveHLayout->addWidget(autoSaveLabel);
autoSaveHLayout->addWidget(autoSaveSpinBox);
QVBoxLayout *vLayout = new QVBoxLayout(this);
vLayout->addWidget(uiGroupBox);
vLayout->addWidget(systemGroupBox);
}
void EnigmaConfigGeneralEnvironmentWidget::updateAutoSaveGroupBox(bool autoSaveIsEnabled)
{
qDebug() << "debug " << autoSaveIsEnabled;
//autoSaveLabel->setDisabled(autoSaveIsEnabled);
//autoSaveSpinBox->setDisabled(getAutoSaveIsEnabled());
}
Spotted it:
void EnigmaConfigGeneralEnvironmentWidget::readSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
bool autoSaveIsEnabled = settings.value("autoSaveIsEnabled",bool(true)).toBool();
setAutoSaveIsEnabledSignal(autoSaveIsEnabled);
settings.endGroup();
}
You are calling void setAutoSaveIsEnabledSignal(bool) here (the signal), not void setAutoSaveIsEnabled(bool) (the actual setter). Hence the member variable is still uninitialized.
Reminder for yourself: Don't name signals as if they were setters, use e.g. void autoSaveIsEnabledChanged(bool)