Have a look at this solution
1. Use the QListWidget directly
2. Create a custom QListWidgetItem (say QLWIPushButton)
3. QLWIPushButton is both a QWidget and QListWidgetItem. I have used QWidget so that I can get mouse events.
4. QLWIPushButton will have a QLabel, and QPushButton on it (you can add more if you want)
5. Again one more issue here is, QLabel does not directly support mose clicking (unless used as hyperlink), we have make a custom QLabel (say CustomWidget)
//MainWindow.h
{
...
protected slots:
void buttonClicked(int row);
void labelClicked(int row);
private:
Ui::MainWindow *ui;
};
//MainWindow.h
class MainWindow : public QMainWindow
{
...
protected slots:
void buttonClicked(int row);
void labelClicked(int row);
private:
Ui::MainWindow *ui;
QListWidget* listWidget;
QLineEdit* rowLineEdit;
QLineEdit* widgetLineEedit;
};
To copy to clipboard, switch view to plain text mode
//MainWindow.cpp
MainWindow
::MainWindow(QWidget *parent
) , ui(new Ui::MainWindow)
{
ui->setupUi(this);
rowLineEdit
= new QLineEdit("Click a row in List Widget", widget
);
widgetLineEedit
= new QLineEdit("Click a row in List Widget", widget
);
layout->addWidget(row_label, 0 , 0, 1, 1);
layout->addWidget(rowLineEdit, 0 , 1, 1, 1);
layout->addWidget(widget_label, 1 , 0, 1, 1);
layout->addWidget(widgetLineEedit, 1 , 1, 1, 1);
widget->setLayout(layout);
dock->setWidget(widget);
addDockWidget(Qt::RightDockWidgetArea, dock);
// Custom QListWidgetItem in QListWidget
QLWIPushButton
* new_item1
= new QLWIPushButton
(QString("Custom QListWidgetItem 1"), listWidget
);
QLWIPushButton
* new_item2
= new QLWIPushButton
(QString("Custom QListWidgetItem 2"), listWidget
);
listWidget->setAlternatingRowColors(true);
// Make itemClicked Connection
connect(new_item1, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
connect(new_item1, SIGNAL(labelClicked(int)), this, SLOT(labelClicked(int)));
// Make itemClicked Connection
connect(new_item2, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
connect(new_item2, SIGNAL(labelClicked(int)), this, SLOT(labelClicked(int)));
setCentralWidget(listWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::buttonClicked(int row)
{
rowLineEdit
->setText
(QString("%1").
arg(row
));
widgetLineEedit->setText("QPushButton");
}
void MainWindow::labelClicked(int row)
{
rowLineEdit
->setText
(QString("%1").
arg(row
));
widgetLineEedit->setText("Custom QLabel");
}
//MainWindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget* dock = new QDockWidget("Display Widget");
QWidget* widget = new QWidget(this);
QGridLayout* layout = new QGridLayout(widget);
QLabel* row_label = new QLabel("Row", widget);
QLabel* widget_label = new QLabel("Widget", widget);
rowLineEdit = new QLineEdit("Click a row in List Widget", widget);
widgetLineEedit = new QLineEdit("Click a row in List Widget", widget);
layout->addWidget(row_label, 0 , 0, 1, 1);
layout->addWidget(rowLineEdit, 0 , 1, 1, 1);
layout->addWidget(widget_label, 1 , 0, 1, 1);
layout->addWidget(widgetLineEedit, 1 , 1, 1, 1);
widget->setLayout(layout);
dock->setWidget(widget);
addDockWidget(Qt::RightDockWidgetArea, dock);
// Custom QListWidgetItem in QListWidget
listWidget = new QListWidget(this);
listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 1")));
QLWIPushButton* new_item1 = new QLWIPushButton(QString("Custom QListWidgetItem 1"), listWidget);
listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 2")));
listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 3")));
QLWIPushButton* new_item2 = new QLWIPushButton(QString("Custom QListWidgetItem 2"), listWidget);
listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 4")));
listWidget->setAlternatingRowColors(true);
// Make itemClicked Connection
connect(new_item1, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
connect(new_item1, SIGNAL(labelClicked(int)), this, SLOT(labelClicked(int)));
// Make itemClicked Connection
connect(new_item2, SIGNAL(buttonClicked(int)), this, SLOT(buttonClicked(int)));
connect(new_item2, SIGNAL(labelClicked(int)), this, SLOT(labelClicked(int)));
setCentralWidget(listWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::buttonClicked(int row)
{
rowLineEdit->setText(QString("%1").arg(row));
widgetLineEedit->setText("QPushButton");
}
void MainWindow::labelClicked(int row)
{
rowLineEdit->setText(QString("%1").arg(row));
widgetLineEedit->setText("Custom QLabel");
}
To copy to clipboard, switch view to plain text mode
//CustomWidget.h
class CustomWidget
: public QLabel{
Q_OBJECT
public:
explicit CustomWidget
(const QString
& text,
QWidget *parent
= 0);
signals:
void released(void);
void clicked(void);
protected:
private:
bool mousePressed;
};
//CustomWidget.h
class CustomWidget : public QLabel
{
Q_OBJECT
public:
explicit CustomWidget(const QString& text, QWidget *parent = 0);
signals:
void released(void);
void clicked(void);
protected:
void mousePressEvent(QMouseEvent* e);
void mouseReleaseEvent(QMouseEvent* e);
private:
bool mousePressed;
};
To copy to clipboard, switch view to plain text mode
//CustomWidget.cpp
CustomWidget
::CustomWidget(const QString
& text,
QWidget* parent
) , mousePressed(false)
{
;
}
{
mousePressed = true;
}
{
emit released();
if(mousePressed)
{
emit clicked();
mousePressed = false;
}
}
//CustomWidget.cpp
CustomWidget::CustomWidget(const QString& text, QWidget* parent)
: QLabel(text, parent)
, mousePressed(false)
{
;
}
void CustomWidget::mousePressEvent(QMouseEvent* e)
{
mousePressed = true;
}
void CustomWidget::mouseReleaseEvent(QMouseEvent* e)
{
emit released();
if(mousePressed)
{
emit clicked();
mousePressed = false;
}
}
To copy to clipboard, switch view to plain text mode
//QLWIPushButton.h
{
Q_OBJECT;
public:
signals:
void labelClicked(int row);
void buttonClicked(int row);
private slots:
void labelClick(void);
void buttonClick(void);
};
//QLWIPushButton.h
class QLWIPushButton : public QObject, public QListWidgetItem
{
Q_OBJECT;
public:
explicit QLWIPushButton(const QString text, QListWidget* view);
signals:
void labelClicked(int row);
void buttonClicked(int row);
private slots:
void labelClick(void);
void buttonClick(void);
};
To copy to clipboard, switch view to plain text mode
//QLWIPushButton.cpp
{
CustomWidget* Label = new CustomWidget(text, widget);
connect(Label, SIGNAL(released()), this, SLOT(labelClick()));
connect(button, SIGNAL(released()), this, SLOT(buttonClick()));
layout->addWidget(Label, 0, 0);
layout->addWidget(button, 0, 1);
widget->setLayout(layout);
view->setItemWidget(this, widget);
setSizeHint(widget->sizeHint());
}
void QLWIPushButton::labelClick(void)
{
if(listWidget())
emit labelClicked(listWidget()->row(this));
}
void QLWIPushButton::buttonClick(void)
{
if(listWidget())
emit buttonClicked(listWidget()->row(this));
}
//QLWIPushButton.cpp
QLWIPushButton::QLWIPushButton(const QString text, QListWidget* view)
: QListWidgetItem(view)
{
QWidget* widget = new QWidget(view);
QGridLayout* layout = new QGridLayout(widget);
QPushButton* button = new QPushButton(text, widget);
CustomWidget* Label = new CustomWidget(text, widget);
connect(Label, SIGNAL(released()), this, SLOT(labelClick()));
connect(button, SIGNAL(released()), this, SLOT(buttonClick()));
layout->addWidget(Label, 0, 0);
layout->addWidget(button, 0, 1);
widget->setLayout(layout);
view->setItemWidget(this, widget);
setSizeHint(widget->sizeHint());
}
void QLWIPushButton::labelClick(void)
{
if(listWidget())
emit labelClicked(listWidget()->row(this));
}
void QLWIPushButton::buttonClick(void)
{
if(listWidget())
emit buttonClicked(listWidget()->row(this));
}
To copy to clipboard, switch view to plain text mode
Bookmarks