i just can find any example in the internet how to loop and get each item in the QListWidget
int count = listWidget->count();
for(int index = 0;
index < count;
index++)
{
QListWidgetItem * item = listWidget->item(index);
// A wild item has appeared
}
The foreach thing is totally different, I think.
If you want more info on that, look at this
http://doc.qt.digia.com/4.2/containers.html#the-foreach-keyword
scroll down to where it talks about the foreach keyword.
Special thanks to Tomalak Geret'kal for adding the proper characters which my keyboard is unable to produce :)
Due to so many upvotes on this, i'll explain the foreach macro here as well.
foreach is a Qt specific C++ addition, implemented using the preprocessor. If you want to disable the thing, just add CONFIG += no_keywords to your XX.pro file.
Qt makes a copy of the list being iterated, but don't worry about performance. Qt containers use implicit sharing, where the actual contents are not copied. Think of it as two reference variables using the same actual variable. This makes it possible to modify the list you are iterating over, without messing up the loop. Note that modifying the list forces Qt to make a copy of the actual contents of the list the first time it's modified.
foreach can be used to loop over all Qt basic containers, QList QVector QMap QMultiMap and so on. QListWidget is not one of these, so it doesn't work on it, sadly. To make matters worse, QListWidget doesn't provide a list of all items, only the ones selected. There is a method called items, which would seem to be nice, but is protected.
To loop over selected items, I think this would work
foreach(QListWidgetItem * item, listWidget->selectedItems())
{
// A wild item has appeared
}
The first Google result for "QWidgetList" told me how to do it.
You can use a QWidgetListIt iterator.
QWidgetList wl = get_some_widget_list();
for (QWidget* w = wl.first(); w != 0; w = wl.next()) {
// use w
}
// or
foreach (QWidget* w, wl) {
// use w
}
I'm not quite sure where this foreach comes from. If it's not provided by Qt, it may be a macro expanding to BOOST_FOREACH, for which you'll need to include boost/foreach.hpp.
Related
I have a function who's job is to generate a list of checkboxes based upon the contents of an enum.
void Controller::createCheckBoxes()
{
const QMetaObject &mo = LicenseManager::staticMetaObject;
int index = mo.indexOfEnumerator("Packages");
QMetaEnum metaPackeges = mo.enumerator(index);
for (int i=0; i != metaPackeges.keyCount(); ++i)
{
QString name = (metaPackeges.key(i));
if(!packageCheckBoxes.contains(name))
{
QCheckBox* box = new QCheckBox;
ui->layoutCheckBoxes->addRow(box);
box->setText(name); box->setCheckable(true);
packageCheckBoxes.insert(name, box);
}
}
}
Where "Packages" is the enum containing the packages, and "packageCheckBoxes" is the QMap that is holding the checkboxes for other functions to operate on them, and "layoutCheckBoxes" is a Qt form layout that the checkboxes visually reside in.
Now, the checkboxes appear on screen looking correct, exactly how I intend them too, and inside the layout. However, clicking on them, or asking the code to do anything to them, instantly results in a crash and I can't figure out why. There's no explanation given by "Application Output" other than "The program has finished unexpectedly." which amazingly I figured out on my own without this notice /s
I had checkboxes in "layoutCheckBoxes" before, but they were manually generated from Design, and those worked perfectly. The reason I'm going through this hassle is to allow packages to be easily added through the enum, with various functions performing the operations necessary to generate the required checkboxes and hook them up to their appropriate functions.
The function that calls "createCheckboxes" is in the same source file, as well as the other functions that operate on the check boxes.
I don't have control over the enum, it is what it is, and it's what I need to work with.
I am in the process of developing a html editor in Qt for one of my university assignments, and i am having a problem regarding naming of some variables.
the problem is this:
when the user decides to load their "project" the program iterates through the folder and finds how many .html files are in there, it then creates tabs for them to be displayed in.
I have a custom QTextEdit which has a customer completer and syntax highlighting etc. the problem i am having at the moment is how to create them depending on the number needed.
i create a QStringList of file names:
QStringList m_files;
m_files = aDialog.m_loadDirectory->entryList(QStringList("*.html"),QDir::Files|QDir::NoSymLinks);
then i iterate through each one of the list:
for(int i=0; i<m_files.count();i++)
{
}
and for each one i need to create a new custom QtextEdit
TextEdit *name = new TextEdit;
then add to the tab
tabs->addTab(name,"someTitle");
but as each TextEdit needs to be different for each tab (i think this is correct) i need a different Variable name for each one.
i thought about creating a list/array of TextEdit objects but as i dont know how many i need to use, i could end up easily with too many (wasted memory) or not enough..
any ideas on how i can get around this?
one thought..
would it be possible to create a TextEdit object before the loop
then make a copy of that object in the loop and add the copied object to the tab? (still variable naming problem...)
thanks
but as each TextEdit needs to be different for each tab (I think this is correct)
Yes, you need a different TextEdit in each tab.
I need a different Variable name for each one.
No, you don't need a different variable name for each one. You need different objects, but variable names don't have much to do with that.
A simple:
for (...) {
TextEdit *te = new TextEdit(...);
// set up that text edit in whatever way you need
tabs->addWidget(te, "foo");
}
does exactly what you want. The variable name te is completely irrelevant (it won't even appear in the executable outside of debugging symbols). Each time through the loop, you'll be working on a separate TextEdit instance.
If you need to refer to that TextEdit by name at runtime, you can keep all your widgets in a collection, a QMap for instance.
QMap<QString, QWidget*> all_editors;
...
for (...) {
TextEdit *te = ...;
all_editors[filename] = te;
...
}
You have discarded quickly the only viable solution : put your text edits in a collection. The textedit have to be created with new, so the collection itself will not waste space.
You can use a QPair<QTabWidget*, QTextEdit*> for simplest cases. For more complicated cases create a custom widget, and just make a list of those.
Copying a QObject is a really bad idea. I think the copy constructor is private so you will not even be able to do that
I am using Qt to draw out some data visualisations. I have lines which indicate certain important points along a timeline, but I would like to be able to remove these lines from view so that the information underneath is more clearly visible.
I understand how to remove them from sight, but the problem is how to find which QGraphicsItems in the scene's view are Lines and which are not.
I have tried using
try {
qgraphicsitem_cast<QGraphicsLineItem>(scene->items()[i]);
} catch (...) {
}
But this doesn't even compile. I tried checking the output of qgraphicsitem_cast() to see if was 0, but the compiler didn't like that, either.
This is my most current code:
void Plotter::showHideLines() {
int i;
QGraphicsLineItem l;
for (i = 0; i < scene->items().count(); i++) {
try {
qgraphicsitem_cast<QGraphicsLineItem>(scene->items()[i]);
scene->items()[i]->setVisible(!scene->items()[i]->isVisible());
} catch (...) {
}
}
}
Ignore l, I didn't bother to delete it after trying something else.
I am pretty new to Qt, I have just been learning it over the last few days. Can anybody help?
I wouldn't advise iterating through every scene item, just to hide a certain type as things will get slow when the number of items gets large.
Instead, whenever a line is created, add it to a list. When they need to be hidden, iterate through list and hide them. It takes slightly more memory, but is much faster, safer, and requires less coding.
I am a student programmer using Qt to build a GUI for work. I have ran into an issue; more or less and inconvience of sorts wiith multiple selections in the QTreeWidget. My GUI has a main interface with a QTreeWidget as the central item in this window. Below the QTreeWidget I have several buttons; copy, edit, and delete. As you might've already guessed each one of these buttons correlates to a function that executes the command. My tree widget has the ability to select multiple items; however, when multiple items are selected the only item that is passed through is the last item that was selected. I was hoping that somebody with some more insight in this IDE might be able to point me in the right direction for accomplishing this. Here is the process that is followed when one of these functions is executed.
void InjectionGUI::copyInjection_Clicked(QTreeWidgetItem *itemToCopy)
{
InjectionData copyInjectionData; //first use data from the tree widget row
QString converter = itemToCopy->text(0); //to find the vector item that will be copied
int id = converter.toInt();
int nameNumber;
copyInjectionData = qTreeInjectionData.at(id);
qTreeInjectionData.append(copyInjectionData);
buildTreeWidget();
}
void InjectionGUI::slotInjectionCopy()
{
if(ui->treeWidgetInjections->currentItem() == 0)
{
QMessageBox invalidSelection;
invalidSelection.setText("No row selected to copy");
invalidSelection.setWindowTitle("Error");
invalidSelection.exec();
}
else
{
copyInjection_Clicked(ui->treeWidgetInjections->currentItem());
}
}
I'm not too sure what code will be relevant towards making this change; so if there is additional structure that anyone would like to see please just requested. I'm pretty sure that my problem or my solution is going to lie in the way that I'm using current item. After reviewing the documentation from Qt's website I'm still unsure how I would change this to allow multiple selections to be passed through the function. Please only provide constructive feedback; I'm only interested in learning and accomplishing a solution. Thanks in advance.
UPDATE! SOLVED!!!
Just thought it might be nice to show what this looked like implemented:
QList<QTreeWidgetItem *> items = ui->treeWidgetInjections->selectedItems();
for(int i = 0; i < items.size(); i++)
{
QTreeWidgetItem *qTreeWidgetitem = new QTreeWidgetItem;
qTreeWidgetitem = items.at(i);
copyInjection_Clicked(qTreeWidgetitem);
}
If you need to know which items are selected, you can use
QList<QTreeWidgetItem *> QTreeWidget::selectedItems() const
to have a list of all the currently selected items in the tree.
Then you may call your function once for every item in the list, or you can overload your function to take as argument a QList<QTreeWidgetItem *> and then run through the list inside the called function.
I have a Qt application for which I derived my own model class from QAbstractTableModel. I have implemented the necessary methods as prescribed in the documentation. When I call removeRows method the changes are correct in my View (the rows I wanted to remove are removed).
But somehow, the operations on the model doesn't seem to be propagated to the QList I use in the model to store my data. When I save the values stored in the QList to the disk, it look like nothing was erased from it by removeRows.
Here is what my removeRows implementation looks like (it is based on the code from the book Advanced Qt Programming, Chapter 3, p.125):
bool MyModel::removeRows(int row, int count, const QModelIndex&)
{
beginRemoveRows( QModelIndex(), row, row + count - 1);
for (int i = 0; i < count; ++i) {
mMyQList.removeAt(row);
}
endRemoveRows();
return true;
}
How do I fix this? What did I miss?
Thanks!
Like Frank O. implies, it's hard to know what's going on without seeing some code. But from the sound of it, the values haven't been removed from QList simply because you haven't taken them out. When you move from Widget to Model/View classes, you have to do this yourself. I.e., in your removeRows() method you must remove the rows from the QList 'by hand'.
It turns out that nothing was wrong with my implementation of removeRows.
The save method was called by my unit tests just before showing my dialog. The dialog was not calling the save method at all.
No wonder the change were visible in the View and not in the output file...