Put item of QTreeWidgetItem into variable - c++

This is the code that i do:
`void Tabella::on_pushButton_clicked()
{
int risultato;
risultato = ui->treeWidget->currentItem()->text(2);
ui->out_3->setText(QString::number(risultato));
}`
Are the any ways to assign the code into variable? Like that?
P.S I'm new in QT, i worked with c++ and java.

Try this:
ui->out_3->setText(ui->treeWidget->currentItem()->text());
You make take a look at this question

Related

C++ barrier function cannot be assign (windows)

I wanted to create something simple like void Function(struct str) to calculate paralel in barriers sync, but seems to not be so simple, so I followed this:
https://learn.microsoft.com/en-us/windows/win32/procthread/creating-threads
and some other topics, but without success. Couldnt find a solution. Code is all in one file.
Any advise how to fix it? make it work?
Solved by std::thread rewrite
Note how when you cast lpParam to a dectectorData* you should cast like this: (detectorData*)lpParam not (detectorData)lpParam. (Note the * denoting a pointer.)
Since I am using C++, there is already std::thread class.
So my simple task can be done by this code:
void detAndComputeT(detectorData &data) {
Ptr<SIFT> detector = cv::SIFT::create();
detector->detectAndCompute(data.image, Mat(), data.keypoints, data.descriptors);
}
and
std::thread threads2[2];
detectorData dataImage1;
detectorData dataImage2;
dataImage1.image = image1_toCalc;
dataImage2.image = image2_toCalc;
threads2[0] = thread(detAndComputeT, std::ref(dataImage1));
threads2[1] = thread(detAndComputeT, std::ref(dataImage2));
threads2[0].join();
threads2[1].join();

How to modify a Button Content from a function (c++ UWP app)?

Currently learning C++ and using Visual Studio 2017. My UWP app have 10 buttons (named b0-b9) and I want to create a function that will manage the content change of the buttons.
For this I need to pass the button name and the content. I want to modify to the function but I don't know how to do it.
It would look something like this:
void contentButtonChange(Button BtnName, String myString)
{
BntName->Content = myString;
}
Main()
{
.....
contentButtonChange(b0, string1);
contentButtonChange(b1, string2);
contentButtonChange(b2, string3);
.....
}
added note: I'm currently able to change the Content of the button from the Main but I'm unable to write a function that will accept a Button as a parameter. I'm always getting an error no matter what I try.
In the example above BtnName in the function is highlighted with the error: expression must have a pointer or handle type
I found how to do it. I need to add this to my function call:
Windows::UI::Xaml::Controls::Button^ btnName
like this:
void contentButtonChange(Windows::UI::Xaml::Controls::Button^ btnName, Platform::String^ myString)
{
bntName->Content = myString;
}
works now.
You need to use TextBlock to set to the Button.
void contentButtonChange(Button BtnName, string myString)
{
BntName->Content = new TextBlock() { Text = myString };
}

How to reference a form and change properties (height and width) from a function in Qt

I'm starting with Qt trying to migrate from VB6. And now I'm trying to change the size of a window (UI form) from a function, so before doing that in the action that opens the form I do this:
void F::on_actionCte_triggered()
{
Frm_ABM_Ctes *W = new Frm_ABM_Ctes(uF->mdiArea);
W->setAttribute(Qt::WA_DeleteOnClose);
W->setWindowState(Qt::WindowMaximized);
W->showNormal();
int Hi = (this->height()/3) - (W->height()/3);
int Wi = (this->width()/3) - (W->width()/3);
W->setGeometry(Wi,Hi,W->width(),W->height());
}
That works fine, the idea is that if I gonna do a lot of forms I want to call a function where it changes the geometry property of the child form. Like: Function(Parent,child) and then use Parent and Child as dynamic objects in my function (as I do in visual basic or VS)
So I did this:
void F::on_actionCte_triggered()
{
Frm_ABM_Ctes *W = new Frm_ABM_Ctes(uF->mdiArea);
W->setAttribute(Qt::WA_DeleteOnClose);
W->setWindowState(Qt::WindowMaximized);
W->showNormal();
FormS(This,W)
}
Where FormS is in a *.h file (which of course I include) and goes like this:
void FormS(QMainWindow Par, QMdiSubWindow Chi)
{
int Hi = (Par.height()/3) - (Chi.height()/3);
int Wi = (Par.width()/3) - (Chi.width()/3);
Chi.setGeometry(Wi,Hi,Chi.width(),Chi.height());
}
and it gives
error: could not convert 'this' from 'F* const' to 'QMainWindow'
FormS(this,W);
^
I don't know which is the best way to approach to my problem. Is there a way to create a public pointer and change any property of that form, or something like that?
Thanks for taking the time to read my problem any help will be appreciated.
First of all, you are trying to pass pointers into this method, so you'll need to adjust the method to take those pointers. Secondly, I'm not sure what F is (you haven't shown the declaration), but if it's a QMainWindow subclass this will work fine like so:
void FormS(QMainWindow *Par, QMdiSubWindow *Chi)
{
int Hi = (Par->height()/3) - (Chi->height()/3);
int Wi = (Par->width()/3) - (Chi->width()/3);
Chi->setGeometry(Wi, Hi, Chi->width(), Chi->height());
}

C++ Adding new pointer objects to List

I have a data structure defined up here called this:
typedef list <classSpec*> ClassSpecList;
I'm trying to add stuff into the list here based on functions that return certain values of that match the same data type. In one function, I have a list pointer object defined here and I have another statement that calls a function.
ClassSpecList *answer = 0;
classSpec *thisanswer = parseClass(br);
Basically I'm trying to add the results of what thisanswer returns into my main ClassSpecList. Problem is, when I try
answer->push_back(new classSpec (*thisanswer));
It compiles but I get a seg fault
When I try somethign else like:
answer->insert(ClassSpecList.begin(), *thisanswer);
I keep getting primary expression errors and I do not know why. I even tried it with other list made without typedef and I still get those.
Thank you.
You should initialize the pointer answer first, like :
ClassSpecList *answer = new ClassSpecList;
then you can add thisAnswer into this list.
This should work:
ClassSpecList *answer = new ClassSpecList;
answer->push_back(thisAnswer);
as should this, which is usually recommended:
ClassSpecList answer;
answer.push_back(thisAnswer);
If possible, parseClass shouldn't return a pointer, and you should use typedef list <classSpec> ClassSpecList;.

QT Cannot convert 'std::string' to 'std::string*' in initialization

I got some problems in creating my program for mobile using QT C++
When i run it i get this: cannot convert 'std::string' to 'std::string*' in initialization
And theres code for that error:
void rozvrh_b17::pars(string par)
{
data = new std::string*(par);
printf(data->data());
}
//data and par are std::string
//without that new std::string*() it does similiar error
And i ask how to convert std::string to std::string* ??
EDIT:
i made this function to transfer data from one form to another and i need to remember that parameter...
I downvoted you because this question shows no research effort.
string * data = &par;
std::string* is a pointer type. You have a std::string, and it's address is the pointer type you want. This is one of the first principles of using pointers.
What do you really try to do?
If you just want to print the string then this should work:
void rozvrh_b17::pars(string par)
{
printf(par.c_str());
}
If you want to create a copy of string on the heap then you need this:
std::string* data = new std::string(par);
but that doesn't make much sense.
You are trying to assign a string from a pointer to string. In this particular case I see several things.
You don't need a copy at all - simply use par right away
You better make par a const ref: void rozvrh_b17::pars(const string& par)
You should use only string whaen calling new otherwise you create a pointer to string. So do: data = new std::string(par);