How to create custom delegate for QComboBox
-
wrote 28 days ago last edited by
Hello, I am trying to create delegate for QComboBox that displays only image without text.
I have tried to create custom delegate, but I don't understand how to do it at all.
paint() is callled only when QComboBox is expanded.
Can someone please help me and explain how does delegates work?Sorry for my bad english.
-
Hello, I am trying to create delegate for QComboBox that displays only image without text.
I have tried to create custom delegate, but I don't understand how to do it at all.
paint() is callled only when QComboBox is expanded.
Can someone please help me and explain how does delegates work?Sorry for my bad english.
wrote 28 days ago last edited byWhat is your goal?
Delegates work as editor widgets. The delegates activate as you open the drop-down menu.
-
wrote 27 days ago last edited by
Вітаю, пане Олександр
To display images in QComboBox items you can use QIcon class with the addItem methodui->customComboBox->addItem(QIcon(":/img/img0.png"), "Item 0"); ui->customComboBox->addItem(QIcon(":/img/img1.png"), "Item 1"); ui->customComboBox->addItem(QIcon(":/img/img2.png"), "Item 2"); ui->customComboBox->addItem(QIcon(":/img/img3.png"), "Item 3");
The QIcon(":/img/imgX.png") syntax assumes you are using .qrc file.
If you just want images and no text:
ui->customComboBox->addItem(QIcon(":/img/img1.png"), "");
-
Вітаю, пане Олександр
To display images in QComboBox items you can use QIcon class with the addItem methodui->customComboBox->addItem(QIcon(":/img/img0.png"), "Item 0"); ui->customComboBox->addItem(QIcon(":/img/img1.png"), "Item 1"); ui->customComboBox->addItem(QIcon(":/img/img2.png"), "Item 2"); ui->customComboBox->addItem(QIcon(":/img/img3.png"), "Item 3");
The QIcon(":/img/imgX.png") syntax assumes you are using .qrc file.
If you just want images and no text:
ui->customComboBox->addItem(QIcon(":/img/img1.png"), "");
wrote 27 days ago last edited by Oleksandr_Radi@zvoopz I am creating icon selector, after image selection I need to write path to that image to database. I want to store path to image in QComboBox, but do not display that path. I think to achieve it I need to subclass QComboBox, does it is?
-
@zvoopz I am creating icon selector, after image selection I need to write path to that image to database. I want to store path to image in QComboBox, but do not display that path. I think to achieve it I need to subclass QComboBox, does it is?
wrote 27 days ago last edited byMaybe some external model where you map the icon path to the actual image and a widget mapper might be a better idea.
-
wrote 27 days ago last edited by
You can make
enum imageID { Image_None = 0, //large enum values to insert additional items in list if you further would need to //icon of www img0 =10001, img1 = 10002, img2 = 10003, //icon of conflict warning = 12001, error = 12002, info = 12003, //other icons img3 = 14001, img4 = 14002, img5 = 14003, };
Assign items to enum
ui->customComboBox->addItem(QIcon(":/img/img0.png"), QVariant(static_cast<int>(img0)); ui->customComboBox->addItem(QIcon(":/img/img1.png"), QVariant(static_cast<int>(img1)); ui->customComboBox->addItem(QIcon(":/img/warning.png"), QVariant(static_cast<int>(warning)); ui->customComboBox->addItem(QIcon(":/img/error.png"), QVariant(static_cast<int>(error));
void MainWindow::on_customComboBox_activated(){ int customIndex = ui->customComboBox->currentData().toInt(); QString pathToImage; switch(customIndex){ case 10001:{ //img0 selected //pathToImage = specified_path } }
-
You can make
enum imageID { Image_None = 0, //large enum values to insert additional items in list if you further would need to //icon of www img0 =10001, img1 = 10002, img2 = 10003, //icon of conflict warning = 12001, error = 12002, info = 12003, //other icons img3 = 14001, img4 = 14002, img5 = 14003, };
Assign items to enum
ui->customComboBox->addItem(QIcon(":/img/img0.png"), QVariant(static_cast<int>(img0)); ui->customComboBox->addItem(QIcon(":/img/img1.png"), QVariant(static_cast<int>(img1)); ui->customComboBox->addItem(QIcon(":/img/warning.png"), QVariant(static_cast<int>(warning)); ui->customComboBox->addItem(QIcon(":/img/error.png"), QVariant(static_cast<int>(error));
void MainWindow::on_customComboBox_activated(){ int customIndex = ui->customComboBox->currentData().toInt(); QString pathToImage; switch(customIndex){ case 10001:{ //img0 selected //pathToImage = specified_path } }
wrote 26 days ago last edited by@zvoopz said in How to create custom delegate for QComboBox:
ui->customComboBox->addItem(QIcon(":/img/img1.png"), QVariant(static_cast<int>(img1));
ui->customComboBox->addItem(QIcon(":/img/warning.png"), QVariant(static_cast<int>(warning));
ui->customComboBox->addItem(QIcon(":/img/error.png"), QVariant(static_cast<int>(error));Thank you for your response, but I am chosing @Pl45m4 method, because my program may load icons dynamically. But anyway, thank you for help!
-
Maybe some external model where you map the icon path to the actual image and a widget mapper might be a better idea.
wrote 26 days ago last edited by@Pl45m4 Your solution is well suited for my purpose! Thank you!
-
8/8