Unable to add ListViewItem - Windows universal app C++ - c++

I'm writing a windows universal app (store app) in C++ and XAML, and I made a listview which I want to add items to. This works perfectly fine when hard-coding the items, but once I want to add them via a loop, this doesn't work anymore. And I get the error
cannot convert argument 1 from 'const char *' to 'Platform::Object ^'
Could anyone tell me what I'm doing wrong? Thank you
My code:
/* This works */
myListView->Items->Append("Hello, world!");
/* This doesn't work */
const char* strarray[] = { "Hello", "World", "Awesome" };
for (int i = 0; i < sizeof(strarray); i++) {
myListView->Items->Append(strarray[i]);
}

You are using a basic datatype for your string array
const char* strarray[]
So maybe the function xxx->Items->Append() needs to receive a managed datatype which would be the following for example:
array<String^>^ strarray = { "Hello", "World", "Awesome" };

Related

const char* returns true in C++ CLI

since yesterday I have been struggling to turn text into label with code from another class, I came to this:
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
TestApp::UI_Error form("test", "test");
Application::Run(% form);
Using the above code, i display a winapi form that receives "test", "test" as 2x const char* on loading, the problem appears when im trying to set the text in labels using these variables
The code looks like this:
public:
UI_Error(const char* errorText, const char* errorCode)
{
InitializeComponent();
this->testLabel->Text = System::Convert::ToString(errorText);
}
For some reason, each time the return value shown in the win forms window is "true", although it should be "test" here, does anyone know the solution?
I tried to use std::string instead of const char*, unfortunately for some reason i get the error code that a static variable is required :(
Maybe I am wrong here, but System::Convert::ToString() seems not to have a method that accepts a const char* pointer. It looks like it gets cast to something else.
Try this instead:
Text = gcnew System::String(errorText);

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 };
}

Can't convert QString to Const Char*

I'm new to QT so please excuse me if I'm blatantly doing something wrong here, but I've looked at all the questions here on the matter but can't seem to find something that works. I'm trying to have the user create a folder by entering a name for it, and it 'creates' a folder with the name. I say 'create' because it's not exactly creating one, it makes a folder first called "project" before you enter the name, and when you enter a name it renames it. However, when I try and rename the folder with the inputted name it gives me
error:C2664: 'int rename(const char *,const char *)' : cannot convert
argument 1 from 'QString' to 'const char *'
Here's my code:
void MainWindow::on_actionNew_Project_triggered(const char *parameter)
{
//Create project folder
QString projectPath = "D:/Project";
QDir dir(projectPath);
if (!dir.exists()) {
dir.mkpath(projectPath);
}
//Get project name from user
bool result;
QString name = QInputDialog::getText(0, "New Project",
"Enter in project name", QLineEdit::Normal,
"", &result);
if(result && !name.isEmpty()) {
//Rename project folder to user created name
QDir dir(projectPath);
if (dir.exists()) {
rename(projectPath, name); //Gives me error HERE
}
}
}
I would appreciate it if you guys could help, I've been stuck on this for hours.
You could call QString::toStdString(), and then call c_str() to retrieve the const char* from the std::string.
Your code would look something like this:
if (dir.exists()) {
rename(projectPath.toStdString().c_str(), name);
}
Try dir.rename(dir.dirName(), name);
You are trying to invoke a member function without an instance.
Since rename() is a member function of QDir, you need a QDir instance in order to invoke it. So rather than just calling rename() which invokes who knows what, you need to dir.rename().
QDir::rename() actually takes 2 QStrings as parameters, but that other function you are invoking takes two raw strings, so you don't really need to convert the strings, you were just calling the wrong function.
bool QDir::rename(const QString & oldName, const QString & newName)
You are most likely calling rename() from <stdio.h>, which could also work given that the parameters are correct and the OS can rename the directory, in that case you will need to convert to "raw" C-style strings via yourString.toLatin1().constData(). But since you are using Qt, you might as well use the QDir API, which works directly with QString.
If it still doesn't work, then either your input parameters are wrong, or there is something preventing the OS from rename the directory, for example a file currently in use.
Qt FAQ says:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QString str1 = "Test";
QByteArray ba = str1.toLatin1();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
return app.exec();
}

COM : convert 'const GUID*' to const wchar_t*

I am porting some code from VS to mingw C++ . One of the statements in my code is
CFactoryTemplate g_Templates[1] = {
{&CLSID_SystemClock, CSystemClock::CreateInstance}
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
I am getting the following error on the first statement
error: cannot convert 'const GUID* {aka const _GUID*}' to 'const
WCHAR* {aka const wchar_t*}' in initialization
I am completely puzzled by this. I did a little investigation and noticed that
CFactoryTemplate is a class in combase.h . Also my project is UNICODE enabled if that matters. Any suggestions on how to resolve this issue ?
Your code - you say you are porting is wrong, you need to provide different parameters to CFactoryTemplate, the compiler error proves that. Here you will find some sample code to init such array of instances of this class (you dont need to fill all fields):
// list of class ids and creator functions for class factory
CFactoryTemplate g_Templates[2]=
{ { L"Gargle filter" // CFactoryTemplate.m_name
, &CLSID_Gargle // CFactoryTemplate.m_ClsID
, CGargle::CreateInstance // CFactoryTemplate.m_lpfnNew
, NULL // CFactoryTemplate.m_lpfnInit
, &sudGargle // CFactoryTemplate.m_pAMovieSetup_Filter
}
, { L"Gargle filter property page"
, &CLSID_GargProp
, CGargleProperties::CreateInstance
}
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
from https://msdn.microsoft.com/en-us/library/aa451506.aspx
also Hans Passant has given you other example

Error C2440 '=' : cannot convert from 'cli::array<Type> ^' to 'wchar_t'

I got a little problem with my C++/CLI progamm.
I got a few Char arrays wo work without problems.
Header1:
ref class _CGuid{
static const int CIDGR=37;
public:
array<Char>^ cGuid;
array<Char>^ cUuid;
}
Cpp1 -> contruktor:
cGuid = gcnew array<Char>(CIDGR);
some function:
_CGuid::Type(String^ tmpname,String^ tmpid)
{
pcName=tmpname;
cUuid=tmpid->ToCharArray();
}
So this Works Perfectly fine for me without errors.
How ever This doesn’t work:
Other Header:
ref class CStorage{
public:
array<String^>^ names;
array<Char>^ mac;
Other contruktor
names = gcnew array<String^>(100);
mac = gcnew array<Char>(100);
some function 2:
names[k]=tname;
mac[k]=tmac->ToCharArray(); <-------- Error Line
k++;
This line gets the error:
error C2440: '=' : cannot convert from cli::array<Type> ^ to wchar_t
with
[
Type=wchar_t
]
There is no context in which this conversion is possible
So I really don´t know whats the problem here.
The error says it all, actually. ToCharArray returns an array<Char>, which you try to assign to a single Char (= wchar_t) when accessing mac[k].
Did you maybe mean to assign to mac instead?
mac = tmac->ToCharArray();
If so, then this line is redundant:
mac = gcnew array<Char>(100);
Here you allocate memory for mac which you later throw away when you re-assign mac.
here you copy a CLI array coming from the "ToCharArray" in 1 wchar_t of the Mac array!
mac[k]=tmac->ToCharArray(); <-------- Error Line
as you want an array of Mac Address you must allocate it with
mac = gcnew array<array<Char> >(100);
so now you can affect mac[k]