Its my first c++ program and I am trying to start with the right foot. I have set up C++CLR Winforms in visual studio 2017 and created a new project.
Inside this Project I have my Form1 with a Label. I am able to edit this label from code from inside my form1.cpp.
Now, I would like to have my program in the background and have the form as a interface to the user, thus, I would like to avoid putting all the relevant code inside the form. For example, If i code a simple counter, that displays the value, I do not want to have the counter inside my cpp file. I would like to have it in my main(), and use the form just do display the value.
now, there are a few questions raising with this:
is the main() the one i find in 'sourcefiles/CppCLR_WinformsProject.cpp/main()'?
is it even a good idea to put things into the main() or should it stay empty and would I have to put my code in separate classes?
how can I access the label1 on form1 from the main or any other given class?
Any help appreciated.
Related
I'm new to QT and this is probably (I hope) easy to solve, but i really dont know how to do it.
My program is quite easy, mainwindow with few buttons, but what my problem is:
In main.cpp I load a list from file and save it in a pointer (ex: listPointer) and I need to acces the info, from that list from many different .ui and .cpp (I mean, the Qt Class Form Pack, .ui .h .cpp).
I donĀ“t know which way is the best to do this. In mainwindow.h I added a list pointer to get the reference and in the .cpp acces it via this->listPointer and that works, but when i try to acces it from other Qt class (ex: Search.cpp) my app just crashes, obviusly becouse I'm trying to access wrong memory block.
So my problem is, how do I share this list to all the QT Objects.
I hope you understand my problem, english is not my native so maybe u dont.
If your main window exists throughout the life of your application and you only need a one instance of your main window, you can simply make your main window class singleton. You can keep your list pointer as a member of main window and add a getter method to it.
Then you can access your list from anywhere of your application like this
QList<String>* list = MyMainWindow::getInstance()->getList();
Well I'm programming a little game on visual studio with c++ and windows forms. I know that you can't have new pages within a window or atleast there is no feature for it. So my question is, is there any workaround to do so?.. or I'm totally wrong with that.
If it's not really clear what I mean then let me try to explain it a little bit. Most of you know those installation executives where you press the NEXT button and another pages pops up within the same window. That's exactly what I want.
I hope you can help me,thanks in advance.
Use a TabControl object and in the constructor of your form add
tabControl1.ItemSize = new System.Drawing.Size(0, 1);
tabControl1.Multiline = true;
tabControl1.SizeMode = TabSizeMode.Fixed;
Lines above make TabPages without the possibility to be selecetd by user.
Then you can create TabPages and switch them using SelectedIndex property of TabControl
Change the apparearance of tiles to Button.
The code is c#, is simple to be translated to c++.
I'm trying to create a simple program, if you can even call it that.
It has a main dialog and inside an edit box/control thing. picture
When I try to debug (I'm using MS Visual Studio 2013) I get the "I-beam" (the flashing text-thing pointer) but I can't input any text. picture
Questions: Why? How to fix?
Also, later on I want to try writing the inputted text into a txt file. I've tried searching for it, and found that you have to use the string method... Beyond that I could not understand the explanations ;(
CODE:
void Cedit2textDlg::OnEnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialogEx::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
program is called edit2text. This is the ONLY part about the edit box I have in my main edit2textDlg.cpp file. The .rc you can see in the first picture.
I have found a somehow similar problem that was solved using dlg.DoModal(); and creating a variable for the edit box with a value-category, but could not understand how and why I should use it.
I'm fairly new to Qt, and am trying to wrap my head around signals and slots. Though I've figured out how to create custom slots, I've yet to figure out how do update the GUI from my C++ code. I realized that the entire UI, which I created in the designer, is only written in what appears to be XML based UI code. Do I have to handwrite my own Qt C++ UI in order to update the interface, or can I somehow use C++ to update the XML based UI? I'm just looking to add a widget to the main form on a button click. Any help is appreciated. Thanks!
To add a widget to a form you can simply do
ui->layout()->addWidget(new Widget);
XML is used by QtDesigner as a mean to create, update, and persist your GUI, enabling a visual approach to development, but in the end you can build your application entirely without it.
you dont havfe to update the xml of UI . you can inherit the ui into your own class using setupUi
http://crpppc19.epfl.ch/doc/qt4-doc-html/html/qwidget.html#setupUi
now in your C++ class you can update the widgets like changing label text, lineEdit text, setting up spinbox value using Signals & slot.
That xml is used by Qt designer and outside of designer it's only used by uic as a source to generate C++ code, so everything that you do in the designer end-up as C++ code, so it can be updated (or done completely) in C++ code (you can look into the code and see that you most likely have a member called ui that is most likely a pointer, but it can also be an instance of a C++ class that is generated for you by uic, and trough that ui member you can access the widgets and layouts that you created in the designer).
A generic resource that i recommend you to read can be found here and then (if you still can't figure it out) ask a specific question about what exactly are you trying to achieve.
LE: that link contains 3 methods to use the generated code into C++ code, don't try all of them into your working project, you may choose one method or use the default one (ui member pointer)
I should find another interest because this one is taking the life out of me quickly. Seems like a lot of people are confused about the intricacies of MFC code, including me. I have an MFC Dialog Box application that creates several dialogs that you navigate to using the typical back or next function. Along the way you collect data via radio group buttons, list boxes and various other controls. For the most part I understand how to get a handle on the data by using the m_ variables provided by the AFX maps throughout the code for each distinct dialog. At the end - and sometimes during - the data collection/selection process gathered by dialogs, I need to do things with what has been collected. I may need to take the data from one dialog and modify the next based on the previous. It seems like when you move through the dialogs the data from the last is lost unless you save it somehow. I know that there are dozens of ways to do this and I have toyed with several of them, from object passing, to creating new classes, new structures, global variables, pointers, whatever.... My concern is, I need a data structure of some sort to stay up and active in memory long enough for my user code to do something with it. That is the problem I think, I don't know in MFC how to deal with this. I have currently decided to go with a struct called dlg_DataHandler (to house collected data from each dialog) with a few test members in a .h file. It has been typedef'd as a pointer. I am creating a variable of this type and setting it = new dlg_DataHandler, but the data isn't getting passed around like I want from dialog to dialog. One thing that I wonder about is, I don't know exactly where to place the "new" statement for creating the variable. Its as if data is not flowing to and from the structure as it should. Anyway here is some of the code:
// file1.h
typedef struct dlg_DataHandler {
int var;
char* String;
int RepetitionRadio; // radio button data
constructor here
} *dlgDataHandler;
extern dlgDataHandler DlgData;
//*****************
// file2.cpp
dlg_DataHandler DlgData = new dlg_DataHandler; // not located anywhere in peticular just in the code since I DON'T KNOW where to put it. DlgData->member gets loaded in the dialog .cpp files to try collect data, but it doesnt seem to be passing data across the different windows.
Put the variable in your main application class (the one derived from CWinApp) and call new in InitInstance(). You can then use AfxGetApp() to gain access to the application instance, and so your variable, from anywhere else in the code.