How to get a widget position in a grid layout
-
wrote on 3 Jun 2022, 07:03 last edited by
Hi,
So my question is , how can i get a widget position ( row and column) that i have set in my GridLayout .Here is my code :
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { auto *_mainWindow = new QWidget(this); auto *BoxLay = new QHBoxLayout(_mainWindow); auto *gridLay = new QGridLayout(this); auto *Cella0 = new OpenGlWidget(this); auto *Cella1 = new OpenGlWidget(this); auto *Cella2 = new OpenGlWidget(this); auto *Cella3 = new OpenGlWidget(this); gridLay->addWidget(Cella0,0,0,1,1);// Cella0 row:0 col:0 gridLay->addWidget(Cella1,0,1,1,1);// Cella1 row:0 col:1 gridLay->addWidget(Cella2,1,0,1,1); / Cella2 row:1 col:0 gridLay->addWidget(Cella3,1,1,1,1);//Cella3 row:1 col:1 BoxLay->addLayout(gridLay); setCentralWidget(_mainWindow); /*now i want something like : qDebug() <<" Cella0 position ---> "<< Cella0->pos(); qDebug() <<" Cella1 position ---> "<< Cella1->pos(); qDebug() <<" Cella2 position ---> "<< Cella2->pos(); qDebug() <<" Cella3 position ---> "<< Cella3->pos(); so when i run it, it should be : Cella0 position ---> 0,0 Cella1 position ---> 0,1 Cella2 position ---> 1,0 Cella3 position ---> 1,1 i've tried with : pos() , and basically all the methods that come after the "->". Please help me understand what am i doing wrong. Thank you. */
-
I still don't understand the issue. Simply pass the row/column to a function of your OpenGlWidget, save this information as a member and use it later.
-
Why do you need it? You set it so you already know it.
You can only iterator through QGridLayout::itemAtPosition() and check with your widget you're looking for.
-
wrote on 3 Jun 2022, 07:16 last edited by
I'm working on a project where i drag and drop some icons from a list in those widget (Cella0 ect...),and i need to write a file that tells me the widget (row and col ) of where i drop the icon , so , in order to try to accomplish that i was searching how can i show in the qDebug how to get those information of a specific widget.
-
You can also pass the information to your custom widgets and save it there so you can access it later on.
-
wrote on 3 Jun 2022, 07:58 last edited by
i will show you my code , so maybe you can tell me what i'm doing wrong, thanks
mainwindow.cpp #include "mainwindow.h" //#include "ui_mainwindow.h" #include "list.h" #include "openglwidget.h" #include <QPushButton> #include <QGridLayout> #include <QtWidgets> #include <QMessageBox> #include <QLabel> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { auto *_mainWindow = new QWidget(this); auto *BoxLay=new QHBoxLayout(_mainWindow); auto *gridLay=new QGridLayout(this); auto *list=new List(this); auto *Cella0 = new OpenGlWidget(this); auto *Cella1 = new OpenGlWidget(this); auto *Cella2 = new OpenGlWidget(this); auto *Cella3 = new OpenGlWidget(this); auto *Cella4 = new OpenGlWidget(this); auto *Cella5 = new OpenGlWidget(this); auto *Cella6 = new OpenGlWidget(this); auto *Cella7 = new OpenGlWidget(this); auto *Cella8 = new OpenGlWidget(this); auto *Cella9 = new OpenGlWidget(this); auto *Cella10 = new OpenGlWidget(this); auto *Cella11 = new OpenGlWidget(this); //bottoni del grid layout auto *writeButton=new QPushButton("Ok"); auto *resetButton=new QPushButton("Res"); Cella0->setFixedSize(50,50); Cella1->setFixedSize(50,50); Cella2->setFixedSize(50,50); Cella3->setFixedSize(50,50); Cella4->setFixedSize(50,50); Cella5->setFixedSize(50,50); Cella6->setFixedSize(50,50); Cella7->setFixedSize(50,50); Cella8->setFixedSize(50,50); Cella9->setFixedSize(50,50); Cella10->setFixedSize(50,50); Cella11->setFixedSize(50,50); writeButton->setFixedSize(50,30); resetButton->setFixedSize(50,30); gridLay->addWidget(Cella0,0,0,1,1); gridLay->addWidget(Cella1,0,1,1,1); gridLay->addWidget(Cella2,1,0,1,1); gridLay->addWidget(Cella3,1,1,1,1); gridLay->addWidget(Cella4,2,0,1,1); gridLay->addWidget(Cella5,2,1,1,1); gridLay->addWidget(Cella6,3,0,1,1); gridLay->addWidget(Cella7,3,1,1,1); gridLay->addWidget(Cella8,4,0,1,1); gridLay->addWidget(Cella9,4,1,1,1); gridLay->addWidget(Cella10,5,0,1,1); gridLay->addWidget(Cella11,5,1,1,1); gridLay->addWidget(writeButton,6,0,1,1); gridLay->addWidget(resetButton,6,1,1,1); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_BARRA_GIU.png"),tr("item0"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_BARRA_SU.png"),tr("item1"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LATO_DESTRO_GIU.png"),tr("item2"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LATO_DESTRO_SU.png"),tr("item3"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LEVA_GETTO_OFF.png"),tr("item4"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LEVA_GETTO_ON.png"),tr("item5"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_ENTRA.png"),tr("item6"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_ESCE.png"),tr("item7"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_RUOTA_A_DX.png"),tr("item8"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_RUOTA_A_SX.png"),tr("item9"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_POSTERIORE_GIU.png"),tr("item10"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_POSTERIORE_SU.png"),tr("item11"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_DX_GIU.png"),tr("item12"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_DX_SU.png"),tr("item13"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_SX_GIU.png"),tr("item14"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_SX_SU.png"),tr("item15"))); list->setViewMode(QListWidget::IconMode); list->setIconSize(QSize(50, 50)); list->setGridSize(QSize(70, 70)); list->setMaximumWidth(500); BoxLay->addWidget(list); BoxLay->addLayout(gridLay); setCentralWidget(_mainWindow); //connect dei bottoni alle funzioni connect(writeButton,SIGNAL(clicked()),this,SLOT(on_writeButton_clicked())); connect(resetButton,SIGNAL(clicked()),this,SLOT(on_resetButton_clicked())); qDebug()<<"posizione cella 0 ->"<< gridLay->itemAtPosition(0,0); } /* struct Pos { int row = -1, col = -1; }; Pos gridPosition(QWidget * widget) { if (! widget->parentWidget()) return {}; auto layout = qobject_cast<QGridLayout*>(widget->parentWidget()->layout()); if (! layout) return {}; int index = layout->indexOf(widget); Q_ASSERT(index >= 0); int _; Pos pos; layout->getItemPosition(index, &pos.row, &pos.col, &_, &_); return pos; // qDebug()<<"posizione CellaN"<<&pos; } */ void MainWindow::on_writeButton_clicked(){ QMessageBox::information(this,"Hello","hai scritto il file "); QFile file("C:/QtCreator/Testing/myfile.txt"); if(!file.open(QFile::WriteOnly | QFile::Text)){ QMessageBox::information(this,"hello","file is not open"); } QTextStream out(&file); // out reference su file // WRITE ON FILE THE NAME OF THE ICON , AND THE WIDGET ROW A ND COL OF THE DROP EVENT file.flush(); file.close(); } void MainWindow::on_resetButton_clicked(){ //RESET TOT QMessageBox::information(this,"Hello","hai cliccato il tasto reset"); { auto *_mainWindow = new QWidget(this); auto *BoxGrid=new QHBoxLayout(_mainWindow); auto *gridLay=new QGridLayout(this); auto *list=new List(this); auto *Cella0 = new OpenGlWidget(this); auto *Cella1 = new OpenGlWidget(this); auto *Cella2 = new OpenGlWidget(this); auto *Cella3 = new OpenGlWidget(this); auto *Cella4 = new OpenGlWidget(this); auto *Cella5 = new OpenGlWidget(this); auto *Cella6 = new OpenGlWidget(this); auto *Cella7 = new OpenGlWidget(this); auto *Cella8 = new OpenGlWidget(this); auto *Cella9 = new OpenGlWidget(this); auto *Cella10 = new OpenGlWidget(this); auto *Cella11 = new OpenGlWidget(this); //bottoni del grid layout auto *writeButton=new QPushButton("Ok"); auto *resetButton=new QPushButton("Res"); Cella0->setFixedSize(50,50); Cella1->setFixedSize(50,50); Cella2->setFixedSize(50,50); Cella3->setFixedSize(50,50); Cella4->setFixedSize(50,50); Cella5->setFixedSize(50,50); Cella6->setFixedSize(50,50); Cella7->setFixedSize(50,50); Cella8->setFixedSize(50,50); Cella9->setFixedSize(50,50); Cella10->setFixedSize(50,50); Cella11->setFixedSize(50,50); writeButton->setFixedSize(50,30); resetButton->setFixedSize(50,30); gridLay->addWidget(Cella0,0,0,1,1); gridLay->addWidget(Cella1,0,1,1,1); gridLay->addWidget(Cella2,1,0,1,1); gridLay->addWidget(Cella3,1,1,1,1); gridLay->addWidget(Cella4,2,0,1,1); gridLay->addWidget(Cella5,2,1,1,1); gridLay->addWidget(Cella6,3,0,1,1); gridLay->addWidget(Cella7,3,1,1,1); gridLay->addWidget(Cella8,4,0,1,1); gridLay->addWidget(Cella9,4,1,1,1); gridLay->addWidget(Cella10,5,0,1,1); gridLay->addWidget(Cella11,5,1,1,1); gridLay->addWidget(writeButton,6,0,1,1); gridLay->addWidget(resetButton,6,1,1,1); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_BARRA_GIU.png"),tr("item0"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_BARRA_SU.png"),tr("item1"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LATO_DESTRO_GIU.png"),tr("item2"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LATO_DESTRO_SU.png"),tr("item3"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LEVA_GETTO_OFF.png"),tr("item4"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LEVA_GETTO_ON.png"),tr("item5"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_ENTRA.png"),tr("item6"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_ESCE.png"),tr("item7"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_RUOTA_A_DX.png"),tr("item8"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_RUOTA_A_SX.png"),tr("item9"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_POSTERIORE_GIU.png"),tr("item10"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_POSTERIORE_SU.png"),tr("item11"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_DX_GIU.png"),tr("item12"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_DX_SU.png"),tr("item13"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_SX_GIU.png"),tr("item14"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_SX_SU.png"),tr("item15"))); list->setViewMode(QListWidget::IconMode); list->setIconSize(QSize(50, 50)); list->setGridSize(QSize(70, 70)); list->setMaximumWidth(500); BoxGrid->addWidget(list); BoxGrid->addLayout(gridLay); setCentralWidget(_mainWindow); // qDebug()<<"Elemento in pos cella 0 ---> "<<Cella0->pos(); //connect dei bottoni alle funzioni connect(writeButton,SIGNAL(clicked()),this,SLOT(on_writeButton_clicked())); connect(resetButton,SIGNAL(clicked()),this,SLOT(on_resetButton_clicked())); } } ---------------------------------------------------------------------------- mainwindow.h #define MAINWINDOW_H #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); private slots: void on_writeButton_clicked(); void on_resetButton_clicked(); // don't know how to clean the layout so i redraw it all . }; -------------------------------------------------------- openglwidget.cpp #include "openglwidget.h" #include "qwidget.h" #include "list.h" OpenGlWidget::OpenGlWidget(QWidget *parent) : QOpenGLWidget(parent) { setAcceptDrops(true); } void OpenGlWidget::dragEnterEvent(QDragEnterEvent *event) { event->accept(); } void OpenGlWidget::dropEvent(QDropEvent *event , QGridLayout *gridLay){ //HOW TO PASS THE GRID LAYOUT HERE (?) , what i have tried is giving me error. const QMimeData *mimeData = event->mimeData(); //contenitore dati settato a formato qabstract item model data list if (mimeData->hasFormat("application/x-qabstractitemmodeldatalist")) { QByteArray arraydati = mimeData->data("application/x-qabstractitemmodeldatalist"); QDataStream stream(&arraydati, QIODevice::ReadOnly); while (!stream.atEnd()) { int row; int col; QMap<int, QVariant> DataMap; stream >> row >> col >> DataMap; QString name = DataMap.value(0).toString(); QIcon icon = DataMap.value(1).value<QIcon>(); m_pixmap = icon.pixmap(icon.availableSizes().first()); update(); qDebug() <<"\n"<<"hai trascinato l'icona -----> "<<name<<"\n"; qDebug()<<"posizione cella 0 ->"<< gridLay->itemAtPosition(0,0); // i think i'm missing something , sorry if you see stupid things. } } else { event->ignore(); } } void OpenGlWidget::paintGL() { QPainter painter(this); painter.drawPixmap(0, 0, m_pixmap); //painter.drawPixmap } ------------------------------------------- openglwidget.h #define OPENGLWIDGET_H #include "mainwindow.h" #include "qevent.h" #include "qpixmap.h" #include <QOpenGLWidget> #include <QGridLayout> #include <QtWidgets> class OpenGlWidget : public QOpenGLWidget { Q_OBJECT public: explicit OpenGlWidget(QWidget *parent = nullptr); protected: void dragEnterEvent(QDragEnterEvent *event) override; void dropEvent(QDropEvent *event)override; void paintGL() override; private: QPixmap m_pixmap; }; //i'm kinda slow so if you can show me a little example i would really appreciate it, thanks.
-
Lifetime Qt Championwrote on 3 Jun 2022, 08:09 last edited by Christian Ehrlicher 6 Mar 2022, 08:10
I don't know what your real problem is. You want to know the row/column of your widgets in the grid layout. Since you add your widgets to the grid, you know the row and the column of them. You can therefore store this information in your widgets and use it later on.
-
i will show you my code , so maybe you can tell me what i'm doing wrong, thanks
mainwindow.cpp #include "mainwindow.h" //#include "ui_mainwindow.h" #include "list.h" #include "openglwidget.h" #include <QPushButton> #include <QGridLayout> #include <QtWidgets> #include <QMessageBox> #include <QLabel> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { auto *_mainWindow = new QWidget(this); auto *BoxLay=new QHBoxLayout(_mainWindow); auto *gridLay=new QGridLayout(this); auto *list=new List(this); auto *Cella0 = new OpenGlWidget(this); auto *Cella1 = new OpenGlWidget(this); auto *Cella2 = new OpenGlWidget(this); auto *Cella3 = new OpenGlWidget(this); auto *Cella4 = new OpenGlWidget(this); auto *Cella5 = new OpenGlWidget(this); auto *Cella6 = new OpenGlWidget(this); auto *Cella7 = new OpenGlWidget(this); auto *Cella8 = new OpenGlWidget(this); auto *Cella9 = new OpenGlWidget(this); auto *Cella10 = new OpenGlWidget(this); auto *Cella11 = new OpenGlWidget(this); //bottoni del grid layout auto *writeButton=new QPushButton("Ok"); auto *resetButton=new QPushButton("Res"); Cella0->setFixedSize(50,50); Cella1->setFixedSize(50,50); Cella2->setFixedSize(50,50); Cella3->setFixedSize(50,50); Cella4->setFixedSize(50,50); Cella5->setFixedSize(50,50); Cella6->setFixedSize(50,50); Cella7->setFixedSize(50,50); Cella8->setFixedSize(50,50); Cella9->setFixedSize(50,50); Cella10->setFixedSize(50,50); Cella11->setFixedSize(50,50); writeButton->setFixedSize(50,30); resetButton->setFixedSize(50,30); gridLay->addWidget(Cella0,0,0,1,1); gridLay->addWidget(Cella1,0,1,1,1); gridLay->addWidget(Cella2,1,0,1,1); gridLay->addWidget(Cella3,1,1,1,1); gridLay->addWidget(Cella4,2,0,1,1); gridLay->addWidget(Cella5,2,1,1,1); gridLay->addWidget(Cella6,3,0,1,1); gridLay->addWidget(Cella7,3,1,1,1); gridLay->addWidget(Cella8,4,0,1,1); gridLay->addWidget(Cella9,4,1,1,1); gridLay->addWidget(Cella10,5,0,1,1); gridLay->addWidget(Cella11,5,1,1,1); gridLay->addWidget(writeButton,6,0,1,1); gridLay->addWidget(resetButton,6,1,1,1); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_BARRA_GIU.png"),tr("item0"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_BARRA_SU.png"),tr("item1"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LATO_DESTRO_GIU.png"),tr("item2"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LATO_DESTRO_SU.png"),tr("item3"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LEVA_GETTO_OFF.png"),tr("item4"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LEVA_GETTO_ON.png"),tr("item5"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_ENTRA.png"),tr("item6"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_ESCE.png"),tr("item7"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_RUOTA_A_DX.png"),tr("item8"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_RUOTA_A_SX.png"),tr("item9"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_POSTERIORE_GIU.png"),tr("item10"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_POSTERIORE_SU.png"),tr("item11"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_DX_GIU.png"),tr("item12"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_DX_SU.png"),tr("item13"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_SX_GIU.png"),tr("item14"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_SX_SU.png"),tr("item15"))); list->setViewMode(QListWidget::IconMode); list->setIconSize(QSize(50, 50)); list->setGridSize(QSize(70, 70)); list->setMaximumWidth(500); BoxLay->addWidget(list); BoxLay->addLayout(gridLay); setCentralWidget(_mainWindow); //connect dei bottoni alle funzioni connect(writeButton,SIGNAL(clicked()),this,SLOT(on_writeButton_clicked())); connect(resetButton,SIGNAL(clicked()),this,SLOT(on_resetButton_clicked())); qDebug()<<"posizione cella 0 ->"<< gridLay->itemAtPosition(0,0); } /* struct Pos { int row = -1, col = -1; }; Pos gridPosition(QWidget * widget) { if (! widget->parentWidget()) return {}; auto layout = qobject_cast<QGridLayout*>(widget->parentWidget()->layout()); if (! layout) return {}; int index = layout->indexOf(widget); Q_ASSERT(index >= 0); int _; Pos pos; layout->getItemPosition(index, &pos.row, &pos.col, &_, &_); return pos; // qDebug()<<"posizione CellaN"<<&pos; } */ void MainWindow::on_writeButton_clicked(){ QMessageBox::information(this,"Hello","hai scritto il file "); QFile file("C:/QtCreator/Testing/myfile.txt"); if(!file.open(QFile::WriteOnly | QFile::Text)){ QMessageBox::information(this,"hello","file is not open"); } QTextStream out(&file); // out reference su file // WRITE ON FILE THE NAME OF THE ICON , AND THE WIDGET ROW A ND COL OF THE DROP EVENT file.flush(); file.close(); } void MainWindow::on_resetButton_clicked(){ //RESET TOT QMessageBox::information(this,"Hello","hai cliccato il tasto reset"); { auto *_mainWindow = new QWidget(this); auto *BoxGrid=new QHBoxLayout(_mainWindow); auto *gridLay=new QGridLayout(this); auto *list=new List(this); auto *Cella0 = new OpenGlWidget(this); auto *Cella1 = new OpenGlWidget(this); auto *Cella2 = new OpenGlWidget(this); auto *Cella3 = new OpenGlWidget(this); auto *Cella4 = new OpenGlWidget(this); auto *Cella5 = new OpenGlWidget(this); auto *Cella6 = new OpenGlWidget(this); auto *Cella7 = new OpenGlWidget(this); auto *Cella8 = new OpenGlWidget(this); auto *Cella9 = new OpenGlWidget(this); auto *Cella10 = new OpenGlWidget(this); auto *Cella11 = new OpenGlWidget(this); //bottoni del grid layout auto *writeButton=new QPushButton("Ok"); auto *resetButton=new QPushButton("Res"); Cella0->setFixedSize(50,50); Cella1->setFixedSize(50,50); Cella2->setFixedSize(50,50); Cella3->setFixedSize(50,50); Cella4->setFixedSize(50,50); Cella5->setFixedSize(50,50); Cella6->setFixedSize(50,50); Cella7->setFixedSize(50,50); Cella8->setFixedSize(50,50); Cella9->setFixedSize(50,50); Cella10->setFixedSize(50,50); Cella11->setFixedSize(50,50); writeButton->setFixedSize(50,30); resetButton->setFixedSize(50,30); gridLay->addWidget(Cella0,0,0,1,1); gridLay->addWidget(Cella1,0,1,1,1); gridLay->addWidget(Cella2,1,0,1,1); gridLay->addWidget(Cella3,1,1,1,1); gridLay->addWidget(Cella4,2,0,1,1); gridLay->addWidget(Cella5,2,1,1,1); gridLay->addWidget(Cella6,3,0,1,1); gridLay->addWidget(Cella7,3,1,1,1); gridLay->addWidget(Cella8,4,0,1,1); gridLay->addWidget(Cella9,4,1,1,1); gridLay->addWidget(Cella10,5,0,1,1); gridLay->addWidget(Cella11,5,1,1,1); gridLay->addWidget(writeButton,6,0,1,1); gridLay->addWidget(resetButton,6,1,1,1); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_BARRA_GIU.png"),tr("item0"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_BARRA_SU.png"),tr("item1"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LATO_DESTRO_GIU.png"),tr("item2"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LATO_DESTRO_SU.png"),tr("item3"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LEVA_GETTO_OFF.png"),tr("item4"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_LEVA_GETTO_ON.png"),tr("item5"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_ENTRA.png"),tr("item6"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_ESCE.png"),tr("item7"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_RUOTA_A_DX.png"),tr("item8"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_PORTELLA_RUOTA_A_SX.png"),tr("item9"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_POSTERIORE_GIU.png"),tr("item10"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_POSTERIORE_SU.png"),tr("item11"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_DX_GIU.png"),tr("item12"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_DX_SU.png"),tr("item13"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_SX_GIU.png"),tr("item14"))); list->addItem(new QListWidgetItem(QIcon(":/pix/images/key_RUOTA_SX_SU.png"),tr("item15"))); list->setViewMode(QListWidget::IconMode); list->setIconSize(QSize(50, 50)); list->setGridSize(QSize(70, 70)); list->setMaximumWidth(500); BoxGrid->addWidget(list); BoxGrid->addLayout(gridLay); setCentralWidget(_mainWindow); // qDebug()<<"Elemento in pos cella 0 ---> "<<Cella0->pos(); //connect dei bottoni alle funzioni connect(writeButton,SIGNAL(clicked()),this,SLOT(on_writeButton_clicked())); connect(resetButton,SIGNAL(clicked()),this,SLOT(on_resetButton_clicked())); } } ---------------------------------------------------------------------------- mainwindow.h #define MAINWINDOW_H #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); private slots: void on_writeButton_clicked(); void on_resetButton_clicked(); // don't know how to clean the layout so i redraw it all . }; -------------------------------------------------------- openglwidget.cpp #include "openglwidget.h" #include "qwidget.h" #include "list.h" OpenGlWidget::OpenGlWidget(QWidget *parent) : QOpenGLWidget(parent) { setAcceptDrops(true); } void OpenGlWidget::dragEnterEvent(QDragEnterEvent *event) { event->accept(); } void OpenGlWidget::dropEvent(QDropEvent *event , QGridLayout *gridLay){ //HOW TO PASS THE GRID LAYOUT HERE (?) , what i have tried is giving me error. const QMimeData *mimeData = event->mimeData(); //contenitore dati settato a formato qabstract item model data list if (mimeData->hasFormat("application/x-qabstractitemmodeldatalist")) { QByteArray arraydati = mimeData->data("application/x-qabstractitemmodeldatalist"); QDataStream stream(&arraydati, QIODevice::ReadOnly); while (!stream.atEnd()) { int row; int col; QMap<int, QVariant> DataMap; stream >> row >> col >> DataMap; QString name = DataMap.value(0).toString(); QIcon icon = DataMap.value(1).value<QIcon>(); m_pixmap = icon.pixmap(icon.availableSizes().first()); update(); qDebug() <<"\n"<<"hai trascinato l'icona -----> "<<name<<"\n"; qDebug()<<"posizione cella 0 ->"<< gridLay->itemAtPosition(0,0); // i think i'm missing something , sorry if you see stupid things. } } else { event->ignore(); } } void OpenGlWidget::paintGL() { QPainter painter(this); painter.drawPixmap(0, 0, m_pixmap); //painter.drawPixmap } ------------------------------------------- openglwidget.h #define OPENGLWIDGET_H #include "mainwindow.h" #include "qevent.h" #include "qpixmap.h" #include <QOpenGLWidget> #include <QGridLayout> #include <QtWidgets> class OpenGlWidget : public QOpenGLWidget { Q_OBJECT public: explicit OpenGlWidget(QWidget *parent = nullptr); protected: void dragEnterEvent(QDragEnterEvent *event) override; void dropEvent(QDropEvent *event)override; void paintGL() override; private: QPixmap m_pixmap; }; //i'm kinda slow so if you can show me a little example i would really appreciate it, thanks.
wrote on 3 Jun 2022, 08:13 last edited by JonB 6 Mar 2022, 08:21How to get a widget position in a grid layout
As @Christian-Ehrlicher has said.
Or, if you don't want to store the position:
for (int row = 0; row < layout->rowCount(); row++) for (int col= 0; col< layout->columnCount(); col++) if (layout->itemAtPosition(row, col) && layout->itemAtPosition(row, col)->widget() == someWidget) return { row, col }; return { -1, -1 };
Or you may be able to figure
row, col
directly from int QLayout::indexOf(QWidget *widget) const. -
I don't know what your real problem is. You want to know the row/column of your widgets in the grid layout. Since you add your widgets to the grid, you know the row and the column of them. You can therefore store this information in your widgets and use it later on.
wrote on 3 Jun 2022, 08:55 last edited by@Christian-Ehrlicher
you said "You can therefore store this information in your widgets and use it later on " , my problem is that i don't need to store it , i have already set where my widgets are , i need to "call " those information in my dropEvent.let me explain better :
in my | openglwidget.cpp |
into the dropEvent-i drop my icon and i made this , in order to show the icon that i dropped:
qDebug() <<"\n"<<"hai trascinato l'icona -----> "<<name<<"\n";
and after that i want to qDebug() the row and the col of the widget of where i dropped the icon. -
@Christian-Ehrlicher
you said "You can therefore store this information in your widgets and use it later on " , my problem is that i don't need to store it , i have already set where my widgets are , i need to "call " those information in my dropEvent.let me explain better :
in my | openglwidget.cpp |
into the dropEvent-i drop my icon and i made this , in order to show the icon that i dropped:
qDebug() <<"\n"<<"hai trascinato l'icona -----> "<<name<<"\n";
and after that i want to qDebug() the row and the col of the widget of where i dropped the icon.@ApprenticeReno said in How to get a widget position in a grid layout:
qDebug() the row and the col of the widget of where i dropped the icon.
Then store this information in your openglwidget class as I already said two times.
-
How to get a widget position in a grid layout
As @Christian-Ehrlicher has said.
Or, if you don't want to store the position:
for (int row = 0; row < layout->rowCount(); row++) for (int col= 0; col< layout->columnCount(); col++) if (layout->itemAtPosition(row, col) && layout->itemAtPosition(row, col)->widget() == someWidget) return { row, col }; return { -1, -1 };
Or you may be able to figure
row, col
directly from int QLayout::indexOf(QWidget *widget) const.wrote on 3 Jun 2022, 09:10 last edited by@JonB
How can i implement you're code into my dropEvent ? i tried to pass my gridLay into the dropEvent like this but it gives me error :void OpenGlWidget::dropEvent(QDropEvent *event , QGridLayout *gridLay)
if my gridLay is in the mainwindow.cpp ,how can i pass it in my openglwidget.cpp and from there to my dropEvent ?
-
I still don't understand the issue. Simply pass the row/column to a function of your OpenGlWidget, save this information as a member and use it later.
1/11