Convert QVector<char*> list into a QStringList - qt

How can I convert a QVector<char*> to a QStringList?

There is no direct way.
QVector<char*> vector;
QStringList list;
foreach (const char * str, vector) {
list << str;
}

I think you can try do it in this way (it works for me if I have a QVector<QString>):
QVector<char*> vector_char;
QStringList myStringList = vector_char.toList();
This code has to work as QStringList has inherited members of QList.
Here is somenthing in the documentation: QVector documentation.
And that is all.
I hope it can help you.

Related

How to convert a QJsonObject to QString

I have a QJsonObject data and want to convert to QString. How can I do this? Searched for help in Qt, it only can convert QJsonObject to QVariantMap...
Thanks in advance.
Remembering when I first needed to do this, the documentation can be a bit lacking and assumes you have knowledge of other QJson classes.
To obtain a QString of a QJsonObject, you need to use the QJsonDocument class, like this: -
QJsonObject jsonObj; // assume this has been populated with Json data
QJsonDocument doc(jsonObj);
QString strJson(doc.toJson(QJsonDocument::Compact));
we can do this in one line
QString strFromObj = QJsonDocument(jsonObject).toJson(QJsonDocument::Compact).toStdString().c_str();
When the macro QT_NO_CAST_FROM_ASCII is enabled, you can do something like:
QJsonDocument doc(jsonObject);
QByteArray docByteArray = doc.toJson(QJsonDocument::Compact);
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
Qstring strJson = codec->toUnicode(docByteArray);
Or better, just use QLatin1String(QByteArray&), based on the example of TheDarkKnight:
QJsonDocument doc(jsonObj);
QByteArray docByteArray = doc.toJson(QJsonDocument::Compact);
Qstring strJson = QLatin1String(docByteArray);

Removing from a std::list with objects

I was taught always to use a pointer list when storing objects, but I started using lists with objects in them instead. So I'm wondering how do I properly remove an object from a list?
The first code example removes from a pointer list and works all right, but the second one gives an error when running. Do I need to use delete when working with list with objects?
using namespace std;
struct SomeObject{
SomeObject(int i){
something=i;
}
int something;
};
void main(){
list<SomeObject*> pointerList;
SomeObject * op = new SomeObject(2);
pointerList.push_back(op);
auto pit = pointerList.begin();
pointerList.erase(pit);
delete *pit;
cout<<(**pit).something<<endl;
list<SomeObject> objectList;
SomeObject o(1);
objectList.push_back(o);
auto oit = objectList.begin();
objectList.erase(oit);
delete &oit;//this one
cout<<oit->something<<endl;
int i;
cin >> i;
}
Also what are down and upsides with doing it this way?
You get the error because oit is an iterator, not a pointer. You use delete on pointers acquired with new. Iterators look like pointers, but they are not pointers. In the example you gave it's just wrong to use delete.
I think it would be more idiomatic (and correct!) this way:
list<SomeObject*> pointerList;
SomeObject * op = new SomeObject(2);
pointerList.push_back(op);
auto pit = pointerList.front();
pointerList.pop_front();
// delete *pit; // wrong
cout << (**pit).something << endl;
list<SomeObject> objectList;
SomeObject o(1);
objectList.push_back(o);
auto oit = objectList.front();
objectList.pop_front();
// delete &oit; // wrong
cout << oit->something << endl;

QString function for only alpha characters?

I am newbie in QT(4.7.4) and I am search for function, that checks an QString for alpha-characters and returns "true" if in this QString contains only characters.
Should I write this simple function myself? :( I hope it exists such function as isText() in VBA, but in Google and documentation I have not found it.
Thanks for answers and sorry for my english :)
You can simply validate the string with a QRegExp class matching an alphanumeric string. I suggest to use it with QValidator to be more clear.
You could use something like this (If your goal is to accept only strings, which contains a single character):
bool containsOnly(QString str, QChar c)
{
for(int i=0; i<str.length(); i++)
if(str.at(i)!=c)
return false;
return true;
}
and in use:
bool b = containsOnly("String", 'a');

How to call "QList<QVariant> QVariant::toList () const"

This is a pretty simple and probably dumb question, but I have forgotten how to use QList QVariant::toList () const
QVariant s = this->page()->mainFrame()->evaluateJavaScript (QString ("Open(%1,%2)").arg (point.x()).arg (point.y()));
List<QVariant> x;
x = s.toList ();
Of course this is wrong, what is the correct way out? :redface:
What you do is almost correct:
QList<QVariant> x = s.toList();
(Note the use of QList instead of List.)
What you're doing is right. May be you can check if the variant contains a list before converting it. E.g:
QVariant variant = list;
if(variant.canConvert(QVariant::List))
{
QList<QVariant> list_1 = variant.toList();
}

How can I get the selected VALUE out of a QCombobox?

In Qt, I can get the selected text of a QComboBox by using the
combobox->currentText() method.
How can I get the selected value?
I searched for help but I couldn't find a method currentData() which I expected to find. I could only find combobox->currentIndex()
Is there a smarter way to do it other than combobox->itemData(combobox->currentIndex())?
Update: This is no longer necessary as of Qt 5. A currentData() method has been added http://doc.qt.io/qt-5/qcombobox.html#currentData-prop
It seems you need to do combobox->itemData(combobox->currentIndex()) if you want to get the current data of the QComboBox.
If you are using your own class derived from QComboBox, you can add a currentData() function.
This one can get the text of current index:
QString cb = cbChoice ->currentText();
you can set QVariant data for all items, then you can get the value when you need it.
there is an example code for this situation:
ui.comboBoxSheetSize->addItem("128 m", QVariant(128));
ui.comboBoxSheetSize->addItem("256 m", QVariant(256));
ui.comboBoxSheetSize->addItem("512 m", QVariant(512));
ui.comboBoxSheetSize->addItem("1024 m", QVariant(1024));
...
void Page::onComboSheetSizeChanged( int index )
{
int value = ui.comboBoxSheetSize->itemData(index).toInt();
}
by the way, i think i misunderstood your question. i think the way you get data is smart enough?
The member function QComboBox::currentData has been added since this question was asked, see this commit
I had same issue
I have solved by
value = self.comboBox.currentText()
print value
This is my OK code in QT 4.7:
//add combobox list
QString val;
ui->startPage->clear();
val = "http://www.work4blue.com";
ui->startPage->addItem(tr("Navigation page"),QVariant::fromValue(val));
val = "https://www.google.com";
ui->startPage->addItem("www.google.com",QVariant::fromValue(val));
val = "www.twitter.com";
ui->startPage->addItem("www.twitter.com",QVariant::fromValue(val));
val = "https://www.youtube.com";
ui->startPage->addItem("www.youtube.com",QVariant::fromValue(val));
// get current value
qDebug() << "current value"<<
ui->startPage->itemData(ui->startPage->currentIndex()).toString();
I'm astonished that there isn't an activated signal and have the same problem. I solved it by making a subclass of QComboBox. I think it's better to avoid having to directly access the object and call its functions because that means more tight coupling and goes against Qt's philosophy. So here's the class I made that works for me.
class SmartComboBox : public QComboBox {
Q_OBJECT
private slots:
void triggerVariantActivated(int index);
public:
SmartComboBox(QWidget *parent);
signals:
void activated(const QVariant &);
};
And the implementation
void SmartComboBox::triggerVariantActivated(int index)
{
activated(itemData(index));
}
SmartComboBox::SmartComboBox(QWidget *parent)
:QComboBox(parent)
{
connect(this, SIGNAL(activated(int)), this, SLOT(triggerVariantActivated(int)));
}
The question is old, but maybe, somebody need an actual answer.
In the QGIS 3.4 you can get the value from the QComboBox with the method currentData().
Example: comboBox.currentData()
Link: https://doc.qt.io/qt-5/qcombobox.html#currentData-prop
I did this
QDir path("/home/user/");
QStringList _dirs = path.entryList(QDir::Dirs);
std::cout << "_dirs_count = " << _dirs.count() << std::endl;
ui->cmbbox->addItem(Files);
ui->cmbbox->show();
You will see with this that the QStringList named _dirs is structured like an array whose members you can access via an index up to the value returned by _dirs.count()
I had the issue and
QString str = m_UI->myComboBox->currentText();
solved this.
if you are developing QGIS plugins then simply
self.dlg.cbo_load_net.currentIndex()
I know I'm very late but for those who still have that problem, it can be solved easily.
I use Qt 5.3 and it works fine. No need to create a function or all that.
int valueComboBox;
valueComboBox = comboBox->currentIndex();
and it works !
Hope it helps !
I confirm the easiest way is to do this:
uiAnalyseAssets::AnalyseAssets(QWidget *parent)
: QWidget(parent)
{
ui.comboBox->addItem("text1");
ui.comboBox->addItem("text2");
...
}
void mainFunction::yourFunction( int index )
{
int value = ui.comboBox->currentText();
}

Resources