Searching Button Text
-
Hey all,
New to the forums, and hope I can be of some assistance in the near future. Thank you all for having a place for development questions.
I am making a small program where I can just have all my executables in one place. So basically I have an icon for the exe and then the button that you click to open the program on the computer.
Question is how can I put a search feature so that when I type into the text edit line, it will find the program I am searching for? If that makes any sense anyway...
Thank you for any help.
-
Hi and welcome to devnet,
You can assign an object name to each of your button and search for it with QObject::findChild
-
Hi and welcome to devnet,
You can assign an object name to each of your button and search for it with QObject::findChild
@SGaist said in Searching Button Text:
Hi and welcome to devnet,
You can assign an object name to each of your button and search for it with QObject::findChild
Thanks for the quick response. I'm very new to this, so any help is good. TYTYTY.
void MainWindow::on_winzip_2_objectNameChanged(const QString &objectName)
{
QPushButton *button = parentWidget->findChild<QPushButton *>("winzip");
}Does that look about close? lol... It doesn't do anything but throw an error, so I guess not. winzip is the program to be open and the object name of the button is winzip. Also, I have it in a scroll area, not a widget?
-
Hey all,
New to the forums, and hope I can be of some assistance in the near future. Thank you all for having a place for development questions.
I am making a small program where I can just have all my executables in one place. So basically I have an icon for the exe and then the button that you click to open the program on the computer.
Question is how can I put a search feature so that when I type into the text edit line, it will find the program I am searching for? If that makes any sense anyway...
Thank you for any help.
@Epiales666 Assuming you have a vector of some sort holding all your buttons, here is a simple example of searching their text:
QPushButton *findButtonByText(const QString &search) { QList<QPushButton *> buttons; // this would be your list of QPushButtons foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
There are limitations to this so I wouldn't necessarily use it as is.
- It only returns the first button it finds that matches the text. This won't work if you have 2 buttons that contain the "search" term. It will only get the first.
- It searches not exact but if it contains the text, i.e. "Some Game" is the button name, and you search "game" it will match, but it will also match "me", etc.
- Don't just use the buttons array, it's there for example, that would be something you had to pass in or something that was declared in your class.
This should give you enough of idea how to do it yourself. :)
-
@Epiales666 Assuming you have a vector of some sort holding all your buttons, here is a simple example of searching their text:
QPushButton *findButtonByText(const QString &search) { QList<QPushButton *> buttons; // this would be your list of QPushButtons foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
There are limitations to this so I wouldn't necessarily use it as is.
- It only returns the first button it finds that matches the text. This won't work if you have 2 buttons that contain the "search" term. It will only get the first.
- It searches not exact but if it contains the text, i.e. "Some Game" is the button name, and you search "game" it will match, but it will also match "me", etc.
- Don't just use the buttons array, it's there for example, that would be something you had to pass in or something that was declared in your class.
This should give you enough of idea how to do it yourself. :)
Well, spent last hour or so messing with it and hunting online. I have this so far, but probably way off course lol...
void MainWindow::on_lineEdit_objectNameChanged(const QString &scrollArea)
{
QPushButton *findButtonByText(const QString &search)
{
QList<QPushButton *> buttons; // this would be your list of
QPushButton *button = new QPushButton("aaalogo");
QPushButton *button = new QPushButton("advancedinstaller");
QPushButton *button = new QPushButton("microsoftword");
QPushButton *button = new QPushButton("winrar");
QPushButton *button = new QPushButton("winzip");foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
on_lineEdit_objectNameChanged - The text area for the search
scrollarea - The holder that holds all the buttonsNot sure if the findbuttonbytext is a name or some function. Couldn't find anything about it. I'll do some more hunting. If someone has time to respond, thank you. If not, I'm patient and will search all night to find out how to get it working if I need to lol. Thanks all!
-
Well, spent last hour or so messing with it and hunting online. I have this so far, but probably way off course lol...
void MainWindow::on_lineEdit_objectNameChanged(const QString &scrollArea)
{
QPushButton *findButtonByText(const QString &search)
{
QList<QPushButton *> buttons; // this would be your list of
QPushButton *button = new QPushButton("aaalogo");
QPushButton *button = new QPushButton("advancedinstaller");
QPushButton *button = new QPushButton("microsoftword");
QPushButton *button = new QPushButton("winrar");
QPushButton *button = new QPushButton("winzip");foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
on_lineEdit_objectNameChanged - The text area for the search
scrollarea - The holder that holds all the buttonsNot sure if the findbuttonbytext is a name or some function. Couldn't find anything about it. I'll do some more hunting. If someone has time to respond, thank you. If not, I'm patient and will search all night to find out how to get it working if I need to lol. Thanks all!
@Epiales666 Ok well you are creating new buttons inside the function I gave you which won't work. You should have those buttons already created by now. Probably stored in a QList or QVector or something?
So basically you are going to search your list of buttons, check their text, compare it to your search term, then get the button if it matches.
So you need an existing list of your buttons to search. You don't need to do the object name stuff because you are just searching based on text, not name. So you can drop that part.
If you want later on I can write up a quick example that does it for you, or if you share your mainwindow code I can fix it for you later on tonight.
-
@Epiales666 Ok well you are creating new buttons inside the function I gave you which won't work. You should have those buttons already created by now. Probably stored in a QList or QVector or something?
So basically you are going to search your list of buttons, check their text, compare it to your search term, then get the button if it matches.
So you need an existing list of your buttons to search. You don't need to do the object name stuff because you are just searching based on text, not name. So you can drop that part.
If you want later on I can write up a quick example that does it for you, or if you share your mainwindow code I can fix it for you later on tonight.
@ambershark said in Searching Button Text:
@Epiales666 Ok well you are creating new buttons inside the function I gave you which won't work. You should have those buttons already created by now. Probably stored in a QList or QVector or something?
So basically you are going to search your list of buttons, check their text, compare it to your search term, then get the button if it matches.
So you need an existing list of your buttons to search. You don't need to do the object name stuff because you are just searching based on text, not name. So you can drop that part.
If you want later on I can write up a quick example that does it for you, or if you share your mainwindow code I can fix it for you later on tonight.
That would be great. Here is my mainwindow code
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPushButton> #include <QtCore> #include <QtGui> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setMinimumSize(QSize(357,670)); setMaximumSize(QSize(357,670)); this->setWindowTitle("Application Pick By ME lol"); QPixmap pix1("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/qtcommunity2.jpg"); ui->qtcommunity->setPixmap(pix1); QPixmap pix2("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/qtcommunity1.jpe"); ui->qtcommunity1_5->setPixmap(pix2); QPixmap pix3("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/microsoftword.jpe"); ui->microsoftword->setPixmap(pix3); QPixmap pix4("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/aaalogocreator.jpe"); ui->aaalogocreator->setPixmap(pix4); QPixmap pix5("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/imageready.jpe"); ui->imageready->setPixmap(pix5); QPixmap pix6("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/photoshop.jpe"); ui->photoshop->setPixmap(pix6); QPixmap pix7("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/advinstaller.jpg"); ui->advinstaller->setPixmap(pix7); QPixmap pix8("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/advsystemcare.jpe"); ui->advsystemcare->setPixmap(pix8); QPixmap pix9("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/apowersoftaudiorec.jpe"); ui->apowersoftaudiorec->setPixmap(pix9); QPixmap pix10("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/autohotkey.jpe"); ui->autohotkey->setPixmap(pix10); QPixmap pix11("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/blender.jpe"); ui->blender->setPixmap(pix11); QPixmap pix12("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/Calendar.jpg"); ui->Calendar->setPixmap(pix12); QPixmap pix13("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/Camera.jpe"); ui->Camera->setPixmap(pix13); QPixmap pix14("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/Calculator.jpe"); ui->Calculator->setPixmap(pix14); QPixmap pix15("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/animfx.jpe"); ui->animfx->setPixmap(pix15); QPixmap pix16("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/camstudio.jpe"); ui->camstudio->setPixmap(pix16); QPixmap pix17("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/cheatengine.jpe"); ui->cheatengine->setPixmap(pix17); QPixmap pix18("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/clipgrab.jpe"); ui->clipgrab->setPixmap(pix18); QPixmap pix19("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/codeblocks.jpe"); ui->codeblocks->setPixmap(pix19); QPixmap pix20("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/daemontools.jpe"); ui->daemontools->setPixmap(pix20); QPixmap pix21("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/datecracker.jpg"); ui->datecracker->setPixmap(pix21); QPixmap pix22("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/chrome.jpe"); ui->googlechrome->setPixmap(pix22); QPixmap pix23("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/xilsoft.jpg"); ui->xilsoftavi->setPixmap(pix23); QPixmap pix24("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/xilsoft2.jpe"); ui->xilsoftdvd->setPixmap(pix24); QPixmap pix25("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/winrar.jpe"); ui->winrar->setPixmap(pix25); QPixmap pix26("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/winzip.jpe"); ui->winzip->setPixmap(pix26); QPixmap pix27("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/autohotkey.jpe"); ui->troubleshooting->setPixmap(pix27); QPixmap pix28("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/microsoftexcel.jpe"); ui->microsoftexcel_2->setPixmap(pix28); QPixmap pix29("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/microsoftfrontpage.jpe"); ui->microsoftfrontpage_2->setPixmap(pix29); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_aaalogo_clicked() { system("C:/MyApps/AAAlogo.ahk"); } void MainWindow::on_adobeimageready_clicked() { system("C:/MyApps/adobeimageready.ahk"); } void MainWindow::on_troubleshooting_2_clicked() { system("C:/MyApps/TROUBLESHOOTING.ahk"); } void MainWindow::on_photoshop_2_clicked() { system("C:/MyApps/adobephotoshop.ahk"); } void MainWindow::on_qtcommunity1_clicked() { system("C:/MyApps/QTCommunity.ahk"); } void MainWindow::on_googlechrome_2_clicked() { system("C:/MyApps/googlechrome.ahk"); } void MainWindow::on_advancedinstall_clicked() { system("C:/MyApps/advancedinstall.ahk"); } void MainWindow::on_winrar_2_clicked() { system("C:/MyApps/winrar.ahk"); } void MainWindow::on_winzip_2_clicked() { system("C:/MyApps/winzip.ahk"); } void MainWindow::on_avi2dvd_clicked() { system("C:/MyApps/xilsoftavi2dvd.ahk"); } void MainWindow::on_dvd2mp4_clicked() { system("C:/MyApps/xilsoftdvd2mp4.ahk"); } void MainWindow::on_microsoftword_2_clicked() { system("C:/MyApps/microsoftword.ahk"); } void MainWindow::on_microsoftexcel_clicked() { system("C:/MyApps/microsoftexcel.ahk"); } void MainWindow::on_microsoftfrontpage_clicked() { system("C:/MyApps/microsoftfrontpage.ahk"); } void MainWindow::on_pushButton_13_clicked() { system("C:/MyApps/advancedsystemcare.ahk"); } void MainWindow::on_animfx_2_clicked() { system("C:/MyApps/animfx.ahk"); } void MainWindow::on_apowersoftaudiorec_2_clicked() { system("C:/MyApps/apowersoftaudiorec.ahk"); }
I was never able to get the code to work with the WHITE spaces... so I have to go through autohotkey program in order to run the exe programs. Frustrating to say the least. And eventually I would like to just be able to ADD the programs instead of manually having to do it, but can't do it that way if the white spaces aren't working correctly. So I'm stuck with manually adding my programs. For me I guess it works.
Thanks again
@ambershark edit: added code tags
-
@ambershark said in Searching Button Text:
@Epiales666 Ok well you are creating new buttons inside the function I gave you which won't work. You should have those buttons already created by now. Probably stored in a QList or QVector or something?
So basically you are going to search your list of buttons, check their text, compare it to your search term, then get the button if it matches.
So you need an existing list of your buttons to search. You don't need to do the object name stuff because you are just searching based on text, not name. So you can drop that part.
If you want later on I can write up a quick example that does it for you, or if you share your mainwindow code I can fix it for you later on tonight.
That would be great. Here is my mainwindow code
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPushButton> #include <QtCore> #include <QtGui> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setMinimumSize(QSize(357,670)); setMaximumSize(QSize(357,670)); this->setWindowTitle("Application Pick By ME lol"); QPixmap pix1("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/qtcommunity2.jpg"); ui->qtcommunity->setPixmap(pix1); QPixmap pix2("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/qtcommunity1.jpe"); ui->qtcommunity1_5->setPixmap(pix2); QPixmap pix3("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/microsoftword.jpe"); ui->microsoftword->setPixmap(pix3); QPixmap pix4("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/aaalogocreator.jpe"); ui->aaalogocreator->setPixmap(pix4); QPixmap pix5("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/imageready.jpe"); ui->imageready->setPixmap(pix5); QPixmap pix6("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/photoshop.jpe"); ui->photoshop->setPixmap(pix6); QPixmap pix7("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/advinstaller.jpg"); ui->advinstaller->setPixmap(pix7); QPixmap pix8("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/advsystemcare.jpe"); ui->advsystemcare->setPixmap(pix8); QPixmap pix9("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/apowersoftaudiorec.jpe"); ui->apowersoftaudiorec->setPixmap(pix9); QPixmap pix10("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/autohotkey.jpe"); ui->autohotkey->setPixmap(pix10); QPixmap pix11("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/blender.jpe"); ui->blender->setPixmap(pix11); QPixmap pix12("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/Calendar.jpg"); ui->Calendar->setPixmap(pix12); QPixmap pix13("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/Camera.jpe"); ui->Camera->setPixmap(pix13); QPixmap pix14("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/Calculator.jpe"); ui->Calculator->setPixmap(pix14); QPixmap pix15("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/animfx.jpe"); ui->animfx->setPixmap(pix15); QPixmap pix16("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/camstudio.jpe"); ui->camstudio->setPixmap(pix16); QPixmap pix17("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/cheatengine.jpe"); ui->cheatengine->setPixmap(pix17); QPixmap pix18("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/clipgrab.jpe"); ui->clipgrab->setPixmap(pix18); QPixmap pix19("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/codeblocks.jpe"); ui->codeblocks->setPixmap(pix19); QPixmap pix20("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/daemontools.jpe"); ui->daemontools->setPixmap(pix20); QPixmap pix21("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/datecracker.jpg"); ui->datecracker->setPixmap(pix21); QPixmap pix22("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/chrome.jpe"); ui->googlechrome->setPixmap(pix22); QPixmap pix23("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/xilsoft.jpg"); ui->xilsoftavi->setPixmap(pix23); QPixmap pix24("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/xilsoft2.jpe"); ui->xilsoftdvd->setPixmap(pix24); QPixmap pix25("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/winrar.jpe"); ui->winrar->setPixmap(pix25); QPixmap pix26("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/winzip.jpe"); ui->winzip->setPixmap(pix26); QPixmap pix27("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/autohotkey.jpe"); ui->troubleshooting->setPixmap(pix27); QPixmap pix28("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/microsoftexcel.jpe"); ui->microsoftexcel_2->setPixmap(pix28); QPixmap pix29("C:/Users/dclar.DESKTOP-JTNNAGR/Desktop/test/ApplicationPicker/icons/microsoftfrontpage.jpe"); ui->microsoftfrontpage_2->setPixmap(pix29); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_aaalogo_clicked() { system("C:/MyApps/AAAlogo.ahk"); } void MainWindow::on_adobeimageready_clicked() { system("C:/MyApps/adobeimageready.ahk"); } void MainWindow::on_troubleshooting_2_clicked() { system("C:/MyApps/TROUBLESHOOTING.ahk"); } void MainWindow::on_photoshop_2_clicked() { system("C:/MyApps/adobephotoshop.ahk"); } void MainWindow::on_qtcommunity1_clicked() { system("C:/MyApps/QTCommunity.ahk"); } void MainWindow::on_googlechrome_2_clicked() { system("C:/MyApps/googlechrome.ahk"); } void MainWindow::on_advancedinstall_clicked() { system("C:/MyApps/advancedinstall.ahk"); } void MainWindow::on_winrar_2_clicked() { system("C:/MyApps/winrar.ahk"); } void MainWindow::on_winzip_2_clicked() { system("C:/MyApps/winzip.ahk"); } void MainWindow::on_avi2dvd_clicked() { system("C:/MyApps/xilsoftavi2dvd.ahk"); } void MainWindow::on_dvd2mp4_clicked() { system("C:/MyApps/xilsoftdvd2mp4.ahk"); } void MainWindow::on_microsoftword_2_clicked() { system("C:/MyApps/microsoftword.ahk"); } void MainWindow::on_microsoftexcel_clicked() { system("C:/MyApps/microsoftexcel.ahk"); } void MainWindow::on_microsoftfrontpage_clicked() { system("C:/MyApps/microsoftfrontpage.ahk"); } void MainWindow::on_pushButton_13_clicked() { system("C:/MyApps/advancedsystemcare.ahk"); } void MainWindow::on_animfx_2_clicked() { system("C:/MyApps/animfx.ahk"); } void MainWindow::on_apowersoftaudiorec_2_clicked() { system("C:/MyApps/apowersoftaudiorec.ahk"); }
I was never able to get the code to work with the WHITE spaces... so I have to go through autohotkey program in order to run the exe programs. Frustrating to say the least. And eventually I would like to just be able to ADD the programs instead of manually having to do it, but can't do it that way if the white spaces aren't working correctly. So I'm stuck with manually adding my programs. For me I guess it works.
Thanks again
@ambershark edit: added code tags
So you can see what I"m doing...
-
So you can see what I"m doing...
kk, I'm up and at em. I'll still look around for examples and getter figured out. Thanks for giving me info and ways to go about it.
-
Since you made your application with Designer, here's a variation on the suggestion of @ambershark using findChildren
QPushButton *findButtonByText(const QString &search) { QList<QPushButton *> buttons = findChildren<QPushButton *>(); foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
You can also use the QRegularExpression overload in order for the find to be more precise but that can done later as an optimisation step.
-
Since you made your application with Designer, here's a variation on the suggestion of @ambershark using findChildren
QPushButton *findButtonByText(const QString &search) { QList<QPushButton *> buttons = findChildren<QPushButton *>(); foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
You can also use the QRegularExpression overload in order for the find to be more precise but that can done later as an optimisation step.
Thank you much. I think the problem I'm having is getting it connected to the actual interface. When I right click on the line edit, where the text would be typed to search, and then pick go to slot? Then choose the object change? Since each button has an object name.
Trying it that way, it gives me local function definitions are illegal on the findtextbyname. As stated, I'm pretty new to all of this, but always wanted to learn. Thanks again for taking the time to reply to the question. You both have been very gracious.
-
QLineEdit::textChanged would likely be a better signal to connect to.
-
QLineEdit::textChanged would likely be a better signal to connect to.
When I try it previous example:
QPushButton *on_lineEdit_textChanged(const QString &search)
{
QList<QPushButton *> buttons;QPushButton ("aaalogo"); foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
moc_mainwindow.obj:-1: error: LNK2019: unresolved external symbol "private: void __cdecl MainWindow::on_lineEdit_textChanged(class QString const &)" (?on_lineEdit_textChanged@MainWindow@@AEAAXAEBVQString@@@Z) referenced in function "private: static void __cdecl MainWindow::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@MainWindow@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)
When I try it your way I have to put the void findChildren(); with it...
void findChildren();
QPushButton *on_lineEdit_textChanged(const QString &search)
{
QList<QPushButton *> buttons = findChildren<QPushButton *>();QPushButton ("aaalogo"); foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr;
ERROR:
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.cpp:447: error: C2059: syntax error : '>'
-
You are missing
MainWindow::
in front ofon_lineEdit_textChanged
. -
LOL, yeah, that makes sense. Didn't notice that. tyty! As stated, I"m new to all of this. Learn Learn Learn :D
Error
cannot convert from 'QPushButton *' to 'MainWindow *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style castHeaded to google now... Thanks for all!
-
error: C2440: 'return' : cannot convert from 'QPushButton *' to 'MainWindow *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style castSorry, didn't post all error.... Apparently something wrong with the return YAY ME lol... <sigh>
-
error: C2440: 'return' : cannot convert from 'QPushButton *' to 'MainWindow *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style castSorry, didn't post all error.... Apparently something wrong with the return YAY ME lol... <sigh>
@Epiales666 Here's what you need for your definition:
QPushButton *MainWindow::on_lineEdit_textChanged(const QString &search)
You added MainWindow but removed the return type of QPushButton* which caused the new problem.
-
@Epiales666 Here's what you need for your definition:
QPushButton *MainWindow::on_lineEdit_textChanged(const QString &search)
You added MainWindow but removed the return type of QPushButton* which caused the new problem.
Okay, I have this in my Header
void MainWindow::on_lineEdit_textChanged(const QString &search);
error on this:
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.h:123: warning: Function declaration MainWindow::on_lineEdit_textChanged contains extra qualification. Ignoring as signal or slot.Then I changed to add the qpushbutton back that I had removed in the cpp file. Thank you for that.
QPushButton *MainWindow::on_lineEdit_textChanged(const QString &search)
{
QList<QPushButton *> buttons;QPushButton ("aaalogo"); foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
Errors:
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.cpp:448: error: C2556: 'QPushButton *MainWindow::on_lineEdit_textChanged(const QString &)' : overloaded function differs only by return type from 'void MainWindow::on_lineEdit_textChanged(const QString &)'
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.cpp:448: error: C2373: 'MainWindow::on_lineEdit_textChanged' : redefinition; different type modifiers
Then I got to thinking, maybe I need it all in the header file? lol.... so I tried it that way and got this error:
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.h:138: warning: Function declaration MainWindow::on_lineEdit_textChanged contains extra qualification. Ignoring as signal or slot.
I know I'm being dumb here, but I don't know much about coding. I am wanting to learn to do so, thus I"m a complete noob. This is a start though, and I do appreciate you both very much.
-
Okay, I have this in my Header
void MainWindow::on_lineEdit_textChanged(const QString &search);
error on this:
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.h:123: warning: Function declaration MainWindow::on_lineEdit_textChanged contains extra qualification. Ignoring as signal or slot.Then I changed to add the qpushbutton back that I had removed in the cpp file. Thank you for that.
QPushButton *MainWindow::on_lineEdit_textChanged(const QString &search)
{
QList<QPushButton *> buttons;QPushButton ("aaalogo"); foreach (QPushButton *b, buttons) { if (b->text().contains(search, Qt::CaseInsensitive)) return b; } return nullptr; }
Errors:
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.cpp:448: error: C2556: 'QPushButton *MainWindow::on_lineEdit_textChanged(const QString &)' : overloaded function differs only by return type from 'void MainWindow::on_lineEdit_textChanged(const QString &)'
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.cpp:448: error: C2373: 'MainWindow::on_lineEdit_textChanged' : redefinition; different type modifiers
Then I got to thinking, maybe I need it all in the header file? lol.... so I tried it that way and got this error:
C:\Users\dclar.DESKTOP-JTNNAGR\Desktop\test\ApplicationPicker\mainwindow.h:138: warning: Function declaration MainWindow::on_lineEdit_textChanged contains extra qualification. Ignoring as signal or slot.
I know I'm being dumb here, but I don't know much about coding. I am wanting to learn to do so, thus I"m a complete noob. This is a start though, and I do appreciate you both very much.
@Epiales666 Ok so it sounds like you are trying to put a return type that isn't void in a slot. That won't work.
So your definition needs to be:
void MainWindow::on_lineEdit_textChanged(const QString &search)
Now your function needs to change because it can't return buttons so you would need to do whatever you want with those buttons inside that slot, or call a subsequent function to do what you need from the slot handler. When I wrote that example function it was not supposed to be a slot but rather something you called when you need to find a button based on search text.
It's ok to be new, we all were at some point. :) It does sound like you don't know C++ at all yet though. You should probably start there before tackling something like Qt. I would recommend getting a good handle on C++ before complicating it with a huge library like Qt.
-
Also give me a little bit and I'll try to write up a function that will do your search and filter for you. Now that I see your code and what you are trying to do (with that screenshot).