Creating an array of buttons - arrays

I am more or less a complete beginner, so please excuse me if that Question does come across as too easy, but I try to create in QT an Array of Bottons. It should look similarly to the Minesweeper Game. Is there another way than to create every single butto or even have, when the program is running, the user choose how big the Array is, for example an 8x8 field.
My Goal is in the end that the user can make somewhat like a map where he can Color each button in the Array differently, but it would be really helpful if someone could tell me just how to create the Array. If possible in Detail, because I am not that used to work with QT or C++.

You can use nested QVector instead of arrays to save your buttons, and here is a peace of code which creates 8*8 buttons view on Qt MainWindow and saves the buttons in QVector, it may help
QGridLayout* layout = new QGridLayout();
ui->centralwidget->setLayout(layout);
QVector<QVector<QPushButton*>> buttons2DVector(8);
for (int i=0;i<8;i++){
buttons2DVector[i].resize(8);
for(int j=0;j<8;j++){
QPushButton *b = new QPushButton("button");
layout->addWidget(b,i,j);
buttons2DVector[i][j] = b;
}
}
Then you can easily access any of your buttons using
buttons2DVector[rowNum][columnNum]->func();

Related

MFC MDI CMFCPropertyGridProperty adding Array for dropdown list merging MP4 tag data

I need some guidance on how to add a drop down list from an array of data after the read info from the MP4 Tag data is parsed. The mechanism I'm using is 100% operational, this is a creature feature addition. The MP4 tag I'm working with is Genre using the ID3V1 standard. There are 191 choices. The way my app was inherited, there are 2 columns, property/value and multiple rows. All of that works. The Genre tag was setup willy nilly so you could basically type whatever and it would store it. I want to remove that and have the 191 elements in the array to choose from using the drop down list. Part of the loading process is that it will pull in whatever was in the MP4 file. So, I want the user to be able to leave as is (most likely tagged by something that supports ID3V2), or select from the populated 191 elements in the dropdown list.
The object looks like this information.h:
protected:
CMFCPropertyGridCtrl m_wndProperties;
The information.cpp looks like this:
void CInformationView::OnInitialUpdate()
{
// create property grid
VERIFY(m_wndProperties.Create(WS_CHILD|WS_VISIBLE|WS_TABSTOP| WS_BORDER, CRect(0,0,0,0), this, 0));
// get document
CMovieDoc *lpkDoc = GetDocument();
ASSERT_VALID_PTR(lpkDoc);
// add properties //Information ORDER Loading <<<<< List shortened Stack overflow question
m_wndProperties.AddProperty(lpkDoc->m_pkArtist);
m_wndProperties.AddProperty(lpkDoc->m_pkTempo);
m_wndProperties.AddProperty(lpkDoc->m_pkGenre);
CView::OnInitialUpdate();
}
The way it pulls the data in from mp4.cpp:
// Genre
m_pkGenre = new CMFCPropertyGridProperty(_T("Genre"),
COleVariant(AfxStringFromUtf8(lptTags->genre), VT_BSTR));
The pointers in mp4.h:
CMFCPropertyGridProperty *m_pkArtist;
CMFCPropertyGridProperty *m_pkTempo;
CMFCPropertyGridProperty *m_pkGenre;
Now I know that pull downs in the 2nd column (Values) can be done because other tags have simple TRUE/FALSE that can be selected, so that tells me it should be possible to create the drop down list I'm looking to do. An example of the TRUE/FALSE looks like this:
// Compilation
m_pkCompilation = new CMFCPropertyGridProperty(_T("Compilation"),
COleVariant((!VALID_PTR(lptTags->compilation)) ? (short)0 : (short)*lptTags->compilation, VT_BOOL));
I've done arrays in C for things like microcontrollers, but not entirely sure if it is the same in C++. I'm thinking it should look like this:
// Initialize Genre Array
const char *genre[4] = { "Rock", "Rap", "Soul", "House" };
The questions are:
How do I create an the array (or does my example above look correct?) to house fixed strings like "Rock", "Rap", "Soul", etc. etc?
How to modify the VALUE row to have the pull down that contains the parsed Genre tag present and then when opened, show the 191 Genre tags where one can be selected to choose from (and ultimately save which is already working).
Actual code, not references to the learn.microsoft.com, the few things I've tried crashes when I try to alter the AddProperties I assume because of the lpkDoc pointers being used.
You should not use a plain old C-style array if you do not have a strong reason to. Use a std::vector instead. You don't even need to indicate the [size].
The same goes for char *. Use a CString or astd::string instead.
const std::vector<CString> = { L"Rock", L"Rap", L"Soul", L"House" };
Don't make your life harder than it needs to be.
2.
for (size_t i= 0; i < genre.size(); i++)
{
auto gnr= genre[i];
lpkDoc->m_pkGenre->AddOption(gnr);
}
or even better
for (auto it : genre)
{
lpkDoc->m_pkGenre->AddOption(it);
}
Important note: You should not have code about properties in your doc object. You are mixing business logic with user interaction logic. Your code in the future will be a nightmare to maintain.
I do not see your lpkDoc->m_pk variable init'ed anywhere, and I bet those pointers are pointing to no man's land.

Actionscript 3.0 - detecting multiple instances already placed on stage and adding them to an array

Right now I'm trying to make a game with dozens of the same, identical block being on stage and when the player hits the block, it stops. Right now, I've figured out how, using arrays, I can create multiple versions of the block automatically in code and place them on the stage, but I was wondering if there's any way that Actionscript can detect multiple instances of the same object placed on the screen by dragging and dropping in Flash, and adding those all to an array?
For example, instead of having the code place the blocks at specific x and y coordinates, I want to place them in the stage myself in the Flash editor and then have them all be a part of the same array or class or something that allows the same hit testing.
Thanks!
Make those blocks instance of a custom class (export to as -> custom class). Run a loop on the display list and check if the objects are instances of that custom class, eg:
for(var i:int = 0; i < this.numchildren; i++)
{
if(this.getChildAt(i) is MyCustomClass)
{
myarray.push(this.getChildAt(i));
}
}

AS3 Array Limits?

I've ran into a weird problem with flash, I have an array of 92 buttons, at first it was all contained in a single array, the buttons up to the first 20th buttons work, the rest don't.
The buttons will take the user to the next scene basically.
So I tried to breakup the array into multiple arrays, so the first array contains the first 20, the second array the 21-40th and so on, the fifth array contains the 81-92 buttons.
The problem now is I will get this error message:
TypeError #1010: A term is undefined and has no properties
and it'll break all the buttons, rendering all the buttons unusable.
Therefore, I commented out the
for (var a=0; a<buttons.length; a++)
{
firstarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
secondarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
thirdarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
fourtharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
//fiftharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
}
in my button spawn function and the buttons from the first to fourth array works flawlessly well except the fifth, which when clicked, nothing happens.
So I tired to create a new function whereby it was only the fiftharray in it and called the new function in the spawner, same error, breaks everything.
Then I thought was there a button naming issue whereby i mistyped something, I took the button names in the fifth array and pasted them into the start of the fourtharray, replacing what was in it plus commenting out the fiftharray from my script.
The once unworkable buttons (81 to 92) worked, but now (61 to 80) didn't.
I tried combining all the arrays using the comarray, but only the first 20 buttons worked.
So I am wondering if is there a fix or something to solve this problem, much help is appreciated!
There is no need to have multiple arrays, remove them. The last array is obviously shorter than the rest and that's messing up your code -> you are pointing to an index that doesn't exist in your last array.
There is actually no need to have an array. You have 92 buttons, that's a nice bunch. Why not to put it to a movieclip instead? What's the need of the array?
Let's assume you select all your buttons and put them inside of movieclip called buttonsClip. Now you can just use this code, without typing all the instance names out to put them to the array (like the tutorial did it... that may work for 8 buttons, but 92... come on :) ):
import flash.display.MovieClip;
import flash.events.MouseEvent;
for(var i:uint=0; i<buttonsClip.numChildren; i++) {
var b:MovieClip = buttonsClip.getChildAt(i) as MovieClip; //Assuming the buttons are movieclips
b.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(e:MouseEvent):void {
trace(e.target.name);
}
I know this Question is currently 8 years old, but maybe this will help another person that is searching for the same thing.
I had pretty much the same problem and in my case it was just that I accidently skipped a number while naming all the Symbols in the libary that I wanted to be in this array. I had 42 Symbols but because of this mistake the function to load the array had 43 and obviously the program was confused about that.

embedded software structure for sublevel screens and menus

I'm new into embedded c programming and I need some advice.
I'm trying to create a menu structure with underneath screens on an embedded system, the OS code and the drawing/menu libraries are ready, but I can't figure out how I'd design this system.
it'll have :
menus and submenus
password screens that user can authenticate before entering some menu items
and the screens beneath them.
I wrote some code that's working and still feels like I'm writing procedural spagetti code. And I'm new to embedded c systems so I don't know if I can use classes or objects inside the code. I'd be grateful if someone shows me the right way to do this. I'm experienced about programming anything else BTW so feel free with the tech talk. ;)
BTW the menu code is structured like this:
menu myMenu;
entry* myMenuEntries;
int selection = 0;
myMenuEntries[0] = [entry definer code];
...
myMenu.entries = (entry *) myMenuEntries;
selection = DisplayMenu(myMenu);
switch(selection){
case 0: exit(); break;
}
How can I create an object in embedded c like it's used in myMenu.entries?
I would definitely use object oriented C here, like you suspect is possible. There are lots of references to how to do oop in C on stack overflow. You make classes out of structs and use function pointers as the member functions which can then be overridden if you want polymorphism.
Each screen could have on onEnter(), for example, to initialize stuff when the screen is loaded, and you could have a drawable class that can encompass strings, primitives, images, etc that know how to draw themselves at any x,y coordinate. You could also have a standard screen object which is made up of a list of the objects on the screen, their coordinates, and contents. These could be initialized at compile time, too, in a big table.
(I've done all this on a DSP using C and ASM)
There are also ready made libraries that can do this. I know that micrium has one. I'm on my phone or I'd Google it and put the link here.

Trouble referencing movieClips

I am working on a flash project that dynamically generates navigation from an XML. For now I am trying to get it to work with arrays so that I can adapt it to xml once I know the logic works. I am new to as3 and learning has been a tiny bit bumpy. I have been searching for a solution to this but many of the examples I have seen have either been too simple to answer my question or too complex to understand since I am on the new side. This is the code I am working with.
var clientList:Array = new Array("Client1","Client2","Client3","Client4","Client5","Client6","Client7","Client8","Client9","Client10","Client11","Client12","Client13","Client14","Client15");
for each (var cName in clientList){
var newClientBtn:btnClientNav = new btnClientNav();
newClientBtn.x = workX;
newClientBtn.y = workY;
workY += newClientBtn.height;
newClientBtn.mcClientName.text = cName;
lContent.mcWork.addChild(newClientBtn.name);
trace(newClientBtn);
}
I can't fingure out how to properly refernce the dynamically created clips. I have a dynamic text box in the button but can't figure out how to reference it properly to change it, then my next issue will be referencing the buttons to make rollover and click code. I know this probably something simple to fix but I have been looking at it for too long and my eyes are starting to cross.. Thank you in advance for any advice you can give.
Why not store the clips you are creating in an object you can access later?
If you want to reference a movie clip by name though, one way to do it is:
var referenceMC:MovieClip = MovieClip(containerMC.getChildByName(“targetMC”));
If it was a text field or a button you were after, I believe you would do the same but instead cast the result of getChildByName to your desired control.
I also believe you want to add the button itself as a child, not pass its name into your addChild call?

Resources