How to add an Item to the specific index in the layout - qt

I am following this example to create a Flow layout:
http://doc.qt.io/qt-4.8/qt-layouts-flowlayout-example.html
This doesn't have addToIndex(int _index) function, so I would like to implement this. Since this layout uses QLayoutItems, I would like to create a function which can insert an Item after the specific index.
How can I get the index of an Item as integer and insert to the layout?
UPDATE:
flowlayout.cpp
FlowLayout *flowLayout = new FlowLayout;
void FlowLayout::insertItem(int index, QLayoutItem *item)
{
if(itemList.size() < index)
{
itemList.append(item);
}
else
{
itemList.insert(index,item);
QRect tmp = this->geometry();
}
update();
}
flowwindow.cpp
void FlowWindow::addLineBreak()
{
flowbreak = new FlowLayoutButton(NULL);
QLayoutItem *item = new QWidgetItem(flowbreak);
flowlayout->insertItem(index, item);
//flowlayout->addWidget(flowbreak);
}
flowlayoutbutton.cpp constructor
FlowLayoutButton::FlowLayoutButton(QWidget *_parent):QWidget(_parent)
{
QBoxLayout *layout = new QHBoxLayout;
flowbreak = new QPushButton(tr("-------Label-------"));
flowbreak->setGeometry(0, 0, 200,20);
layout->addWidget(flowBreak);
setLayout(layout);
}

add the following method:
flowlayout.h:
class FlowLayout : public QLayout
{
public:
void insertWidget(int index, QWidget *w);
// rest of the class from the example
};
flowlayout.cpp:
// new method
void FlowLayout::insertWidget(int index, QWidget *w) {
addWidget(w);
itemList.move(indexOf(w), index);
}
now you can use it by just calling (assuming you have a pointer to the layout named flowLayout):
flowLayout->insertWidget(2,new QPushButton("Button"));

Related

How to switch between two QGraphicsView inside a QWidget

I have two different views to display inside a QWidget window. Each view has a separate QGraphicsScene.
I want to toggle between the two views on button click.
This is my current implementation:
void toggleUi(bool type){
QGraphicsView* currentView;
if(bool){
currentView = getFirstView(); // returns QGraphicsView of first type
}
else{
currentView = getSecondView(); // returns QGraphicsView of second type
}
QLayout* layout = widget->layout ();
if (layout != 0)
{
QLayoutItem *item;
while ((item = layout->takeAt(0)) != 0)
layout->removeItem (item);
delete layout;
}
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(currentView);
}
Issue: Both views are displayed over one another on toggle, even after deleting the layout and adding a new one.
Both views are rendered fine without toggle.
Is there a better/another way to do it?
Use QStackedWidget! Use the addWidget function to add widgets:
QStackedWidget *stackedWidget = new QStackedWidget(this);
stackedWidget->addWidget(getFirstView());
stackedWidget->addWidget(getSecoundView());
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(stackedWidget);
setLayout(layout);
Here's how to switch between the widgets:
void toggleUi(bool type){
if(condition)
stackedWidget->setCurrentindex(0)
else
stackedWidget->setCurrentindex(1)
I hope it helps!

QObjectPicker not working with custom geometry mesh and renderer

I have set up a simple 3d scene with a custom QGeometryRenderer and QGeometry. The custom QGeometry is loaded from a ply file.
class ColorMeshGeometry : public Qt3DRender::QGeometry
{
Q_OBJECT
public:
ColorMeshGeometry(QString meshFile, ColorMeshRenderer *parent);
~ColorMeshGeometry();
void GeometryCenter(QVector3D* center);
};
class ColorMeshRenderer : public Qt3DRender::QGeometryRenderer
{
ColorMeshGeometry* m_geometry;
Q_OBJECT
public:
explicit ColorMeshRenderer(QString meshFile, Qt3DCore::QNode *parent = 0)
{
m_geometry = new ColorMeshGeometry(meshFile, this);
setGeometry(m_geometry);
}
~ColorMeshRenderer();
void ViewCenter(QVector3D* center);
};
And here is the code to set it all up:
uto view = new Qt3DExtras::Qt3DWindow();
auto container = createWindowContainer(view, this);
auto rootEntity = new Qt3DCore::QEntity();
ColorMeshRenderer* mesh = new ColorMeshRenderer(filename, rootEntity);
view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x111111)));
view->camera()->lens()->setPerspectiveProjection(45.0f, view->width()/view->height(), 0.01f, 100000.0f);
view->camera()->setPosition(QVector3D(0.f, -512.f, 500.0f));
view->camera()->setViewCenter(QVector3D(0, 0, 0));
auto material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity);
auto picker = new Qt3DRender::QObjectPicker(rootEntity);
picker->setHoverEnabled(false);
picker->setDragEnabled(false);
auto plyEntity = new Qt3DCore::QEntity(rootEntity);
plyEntity->addComponent(mesh);
plyEntity->addComponent(material);
plyEntity->addComponent(picker);
connect(picker, &Qt3DRender::QObjectPicker::pressed, this, &ViewerWidget::picker_Clicked);
Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
camController->setCamera(view->camera());
view->setRootEntity(rootEntity);
The QObjectPicker::pressed event is never fired. If I use the Qt3DExtras::QTorusMesh instead, the pressed event is fired. What else needs to be implemented for the QObjectPicker to work with a custom mesh?
Edit:
Full sample code can be found here.
The fix for this ended up being the Perspective Projection. The back pane of the project ended up being way to far way, the fix is:
view->camera()->lens()->setPerspectiveProjection(45.0f, view->width()/view->height(), 0.01f, 5000.0f);

Qt - requiring new model rows to be non-empty

I'm making a program where the user can add multiple people (participants) in a list. When the "Add" button is clicked, a new row is added and "edit" is called for the name field. All is well so far, but there is a thing I'd like to implement, and I can't seem to figure out how: when the user closes the editing field (presses enter or escape, clicks elsewhere, etc.) and if the name field remains empty, I'd like the row to be deleted. In other words, a name has to be filled in. Here is what I have so far:
void MainWindow::addParticipant()
{
QList<QStandardItem *> newRow;
newRow << new QStandardItem()
<< new QStandardItem();
participantModel->appendRow(newRow);
participantView->edit(participantModel->index(participantModel->rowCount()-1, 0));
}
Here participantModel is a QStandardItemModel and participantView is a QTreeView. I tried using signals and slots to detect when a row is empty and to delete it, but it hasn't worked and the syntax is elusive to me.
Ideally I'd be able to detect when the name field is not being edited anymore, so that I can delete the row if need be.
Here is ugly but working solution: subclass from QItemDelegate and check input data inside setModelData member function. As far setModelData has a const qualifier you can not modify model inside it, so you need some trick: in the following example the model is modified inside handler of closeEditor signal.
class MainWidget : public QWidget
{
Q_OBJECT
public:
MainWidget ()
{
QStandardItemModel * model = new QStandardItemModel ();
ItemDelegate * delegate = new ItemDelegate ();
table->setItemDelegate (delegate);
connect (delegate, & ItemDelegate::closeEditor, [=](){
if (isEmpty) {
model->removeRow (emptyRow);
isEmpty = false;
emptyRow = -1;
}
});
connect (delegate, & ItemDelegate::cellEdited, [=](const int row){
isEmpty = true;
emptyRow = row;
});
}
bool isEmpty;
int emptyRow;
};
class ItemDelegate : public QItemDelegate
{
Q_OBJECT
signals:
void cellEdited (int) const;
public:
void setModelData (QWidget * widget, QAbstractItemModel * model, const QModelIndex & index) const override
{
if (0 == index.column () ) {
if (QLineEdit * cellWidget = qobject_cast <QLineEdit *> (widget) ) {
if (cellWidget->text ().isEmpty () ) {
emit cellEdited (index.row () );
return;
}
}
}
QItemDelegate::setModelData (widget, model, index);
}
};
Complete example available at GitLab.
The comments/answers posted thus far have urged me to look more into item delegates. Quite embarrassingly, after relatively little googling I found the following solution for my problem:
void MainWindow::addParticipant()
{
QStyledItemDelegate *participantDelegate = new QStyledItemDelegate;
participantView->setItemDelegateForColumn(0, participantDelegate);
QList<QStandardItem *> newRow;
newRow << new QStandardItem()
<< new QStandardItem();
participantModel->appendRow(newRow);
connect(participantDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), this, SLOT(checkRow()));
participantView->edit(participantModel->index(participantModel->rowCount()-1, 0));
}
Apparently the closeEditor signal (only available to delegates) is exactly what I was looking for. When the editor is closed, the slot checkRow() checks if the name field of the participant is empty and decides whether or not to delete the row.

QMdiArea not adding subwindow

I have an one function which is responsible for initializing custom widget and add it in to the MdiArea.When I call this at first time it is working fine.But if i will call again, it is initializing custom widget but not adding in into the MdiArea. I have mentioned that function here:-
void CArbWaveViewWidget::newFile()
{
m_ptrWavePresenter = new CArbWavePresenter;
QMdiSubWindow *subWindow1 = new QMdiSubWindow;
subWindow1->setWidget(m_ptrWavePresenter->getTableView()); // getting customWidget
qDebug()<<"Table View ==="<<m_ptrWavePresenter->getTableView();
subWindow1->setAttribute(Qt::WA_DeleteOnClose);
QMdiSubWindow *subWindow2 = new QMdiSubWindow;
subWindow2->setWidget(m_ptrWavePresenter->getGraphView()); // getting customWidget
qDebug()<<"Graph View ==="<<m_ptrWavePresenter->getGraphView();
subWindow2->setAttribute(Qt::WA_DeleteOnClose);
mdiArea->addSubWindow(subWindow1);
mdiArea->addSubWindow(subWindow2);
}
How can i solve this problem?
1. Declare your QMdiSubWindow
When you declare a QMdiSubWindow, give mdiArea as argument
QMdiSubWindow *subWindow = new QMdiSubWindow(mdiArea);
or you can use setParent ( QWidget * parent )
QMdiSubWindow *subWindow = new QMdiSubWindow();
subWindow->setParent(mdiArea);
2. Create and add your QWidget in your QMdiSubWindow
QWidget *myWidget = new QWidget();
subWindow->setWidget(myWidget);
3. Update QMdiSubWindow content
If you need to update the subwindow content, declare your QMdiSubWindow as class variable, initialize your QMdiArea and QMdiSubWindow and set QWidget
yourClass.h
class yourClass {
public:
yourClass();
void newFile();
private:
QMdiArea *m_area;
QMdiSubWindow *m_subWindow1, *m_subWindow2;
void init();
};
yourClass.cpp
yourClass::yourClass()
{
init();
}
void yourClass::init()
{
m_area = new QMdiArea();
m_subWindow1 = new QMdiSubWindow(m_area);
m_subWindow2 = new QMdiSubWindow(m_area);
// continue to init your QMdiSubWindow
}
void yourClass::newFile()
{
// Set your QWidget (yourWidget) into your QMdiSubWindow
m_subWindow1->setWidget(yourWidget);
m_subWindow2->setWidget(anotherWidget);
}
You need invoke method "show"
QMdiSubWindow *subWindow = new QMdiSubWindow();
subWindow->setParent(mdiArea);
subWindow->setWidget(yourWidget);
subWindow->show();

How to add multiple tables dynamically in scroller in qt

I am adding three tables dynamically in widget containing table widget and labels, but nothing shows on screen, I have tried to do it with vertical layout but it does not expand if i add a new row, so not scrolling.
Is there any other way to get all three tables on a same page with scrolling.
QScrollArea *m_scrollArea =ui->scrollArea_Stats;
m_scrollArea->setWidgetResizable(true);
QWidget *area = new QWidget;
QVBoxLayout *vlay = new QVBoxLayout(m_scrollArea);
area->setLayout(vlay);
StatsWidget *objStatsWidget;
for(int i=0;i<2;i++)
{
objStatsWidget=new StatsWidget(ui->scrollArea_Stats);
vlay->addWidget(objStatsWidget);
}
m_scrollArea->setWidget(area);
here StatsWidget is my custom widget containing 2 lables at top and a table widget
I am adding three tables dynamically but page is not scrolling, vlay is not showing all tables it is just showing what it can show in a page without scrolling.
try rewrite the code as this:
m_scrollArea->setWidgetResizable(true);
QVBoxLayout *vlay = new QVBoxLayout;
StatsWidget *objStatsWidget;
for(int i=0;i<2;i++)
{
objStatsWidget=new StatsWidget(ui->scrollArea_Stats);
vlay->addWidget(objStatsWidget);
}
QWidget *area = new QWidget(m_scrollArea);
area->setLayout(vlay);
m_scrollArea->setWidget(area);
EDIT: i made something like what you are trying to do some time ago..
so: create a custom QWidget with a QVBoxLayout as member.let's call this object "widgetList". then reimplement all method that you need, as addWidget, takeAt etc.. using your layout as a list
finally set widgetList as widget for your scroll area..
let me know..
I made all this because QWidgetList was not enough easy to use and i needed something else that i have omitted here..
I found my piece of code:
class WidgetList : public QWidget
{
Q_OBJECT
public:
WidgetList(QWidget *parent = 0);
~WidgetList();
void addWidget(QWidget*);
void removeWidget(QWidget*);
QList<QWidget*> getListWidget() const;
QWidget* takeAt(int) const;
int count() const;
private:
QVBoxLayout* layout_;
};
.cpp
WidgetList::WidgetList(QWidget *parent)
: /**/QWidget(parent)
/**/,layout_(new QVBoxLayout(this))
{
this->setLayout(layout_);
}
void WidgetList::removeWidget(QWidget* widget)
{
layout_->removeWidget(widget);
}
void WidgetList::addWidget(QWidget* widget)
{
layout_->addWidget(widget);
}
QWidget* WidgetList::takeAt(int index) const
{
return layout_->takeAt(index)->widget();
}
int WidgetList::count() const
{
return layout_->count();
}
this will be your new Widget with layout where to insert your custom widget..
then i put widgetList as widget of QScrollArea:
QScrollArea* scrollArea = new QScrollArea;
widgetList* list = new widgetList(scrollArea);
scrollArea->setWidget(list);
everything works for me..
EDIT 2: i post my main that works good with my previous code:
QScrollArea* scroll = new QScrollArea;
WidgetList* w = new WidgetList(scroll);
QLabel * label = new QLabel("Label1");
QLabel* label2 = new QLabel("label2");
QTableWidget* table = new QTableWidget(10,10);
w->addWidget(label);
w->addWidget(label2);
w->addWidget(table);
scroll->setWidget(w);
scroll->setWidgetResizable(true);
scroll->show();

Resources