×

Discussion Board

Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Creating an Array of Button

    hello friends

    I am creating an array of buttons having 7 rows 7 colums..
    now i trying to display first seven buttons in one row after which it goes to second row and display next seven button..and so on..

    i have wrriten a code for this..its working but problem is that all the buttons are displayed in one row only..so if anyone suggest me to resolve that problem..

    i am sending my code along with this..

    QWidget *centralWidget = new QWidget;
    selectedDate=QDate::currentDate();
    int count=1,i,j;
    QPushButton *button[10][10];
    QHBoxLayout *controlsLayout = new QHBoxLayout;
    for(i=0;i<7;i++)
    {
    for(j=0;j<7;j++)
    {
    if(count<=42)
    {


    button[i][j] = new QPushButton("p");

    button[i][j]->resize(40,40);

    button[i][j]->move(40*j, 40*i);

    button[i][j]->show();

    controlsLayout->addWidget(button[i][j]);

    centralWidget->setLayout(controlsLayout);

    setCentralWidget(centralWidget);

    count++;
    }

    }

    }

    }

  2. #2
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: Creating an Array of Button

    one way to solve is by using QGridLayout .. other is make each row a widget and add to QHBoxLayout

  3. #3
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Re: Creating an Array of Button

    hello skumar

    initially i use gridlayout in palce of QHboxlayout..but result was...all the buttons are come in one colums...i will send u the code u please check it and suggest me where is the error..

    QWidget *centralWidget = new QWidget;

    int count=1,i,j;
    QPushButton *button[10][10];
    QGridLayout *controlsLayout = new QGridLayout;
    for(i=0;i<7;i++)
    {
    for(j=0;j<7;j++)
    {
    if(count<=42)
    {


    button[i][j] = new QPushButton("p");

    button[i][j]->resize(40,40);

    button[i][j]->move(40*j, 40*i);

    button[i][j]->show();

    controlsLayout->addWidget(button[i][j]);

    centralWidget->setLayout(controlsLayout);

    setCentralWidget(centralWidget);

    count++;
    }

    }

    }

    }

  4. #4
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: Creating an Array of Button

    Code:
    controlsLayout->addWidget(button[i][j]);
    for Grid layout void addWidget ( QWidget * widget, int row, int column, Qt::Alignment alignment = 0 )

    so try using
    Code:
    controlsLayout->addWidget(button[i][j],i,j);

  5. #5
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Re: Creating an Array of Button

    Thanks Skumar..its comming know...

  6. #6
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Re: Creating an Array of Button

    hello skumar

    The code u hv given is working properly..the only thing is that ...hw can i decrease the space between each row...moreever hw can i add textBrowser below..after all buttons are arranged in rows and colums..

    with regards
    Anshuman

  7. #7
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: Creating an Array of Button

    The code u hv given is working properly..the only thing is that ...hw can i decrease the space between each row...
    you can play with
    QGridLayout::setSpacing()
    QGridLayout ::setHorizontalSpacing()
    QGridLayout ::setVerticalSpacing()

    moreever hw can i add textBrowser below..after all buttons are arranged in rows and colums..
    sorry did not understand your requirement. can you be more specific might be a image of your requirement will help.

  8. #8
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Re: Creating an Array of Button

    ok skumar...thanks for ur support...now can i use these button in creating my custom calenderWidget....thats means i want to represent each cell of the calender in buttons as want to work it as an calenderWidget...if possible hw can i implement it...pls suggest me..

    with regards
    Anshuman

  9. #9
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: Creating an Array of Button

    you should look for some open source out there if you want to create any common controls / widgets .. mostly somebody some where done that you can improve that or use that as it is .

    coming to Calender widget you can refer to http://qtnode.net/wiki/Custom_widgets

  10. #10
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Re: Creating an Array of Button

    hello sKumar..

    i want to add background image in an button using codding ie by not setting the property of button..so is it possible to set image in button using codding..can u please suggest me

    with regards
    Anshuman

  11. #11
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Re: Creating an Array of Button

    hello skumar

    Actually i am trying to setbackground image in buttons..but its not working..i am Qstyle for that..u plz check the code..
    QString style="QPushButton::forwardBut { width:26px; height:17px;"
    "image: url(icons:right_arrow.png); } ";

    QPushButton *forwardBut=new QPushButton("->");
    forwardBut->setStyleSheet(style);
    with regards
    Anshuman

  12. #12
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: Creating an Array of Button

    you can do it my programtically too m_pushButton->setIcon(QIcon(":/Images/button_back.png"));

  13. #13
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Re: Creating an Array of Button

    Thanks its working..but the icon size is not clearly visible ...so hw to make it clear in the button..

  14. #14
    Regular Contributor
    Join Date
    Apr 2011
    Posts
    53

    Re: Creating an Array of Button

    hello sir

    As per ur suggestion i will be able to create an array of buttons consisting of six rows and seven colums....which look like that.

    <- April -> // Arow sign are in button..representing forward and backward to change month

    M T W Th F S SU //weeks are in label

    1 2 3 4 5 6 7

    8 9 10 11 12 13 14

    .........................................................

    .......................42


    Number in bold are buttons stored in an array of a[6][7]...

    now i am trying to update each button on changing of month...as per calender work..please help me to suggest that hw can i implement this to work it as calender...as far of my code that i had wrritten is given below..from there hw can i proceed please suggest...

    MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
    {

    ui->setupUi(this);

    QWidget *centralWidget = new QWidget;
    selectedDate=QDate::currentDate();
    x=selectedDate.month();
    // QString style="QPushButton::forwardBut { width:26px; height:17px;"
    // "image: url(icons:right_arrow.png); } ";

    int count=1,i,j;
    MonthLbl=new QLabel("MonthDisplay");
    MonthLbl->setText(selectedDate.longMonthName(x));;
    QPushButton *button[10][10],*forwardBut,*backwardBut;

    forwardBut=new QPushButton();

    forwardBut->setIcon(QIcon("C:/Plackal/QtWorkspace/CustomCalender/Images/back-icon.PNG"));

    backwardBut=new QPushButton("<-");
    QGridLayout *controlsLayout = new QGridLayout;
    QGridLayout *hWeek = new QGridLayout;

    QVBoxLayout *v=new QVBoxLayout;
    QHBoxLayout *h=new QHBoxLayout;
    QHBoxLayout *hLbl=new QHBoxLayout;
    QLabel *blankLbl=new QLabel;

    QLabel *blank=new QLabel;

    QLabel *weekLbl[7];
    for(i=1;i<=7;i++)
    {
    weekLbl[i]=new QLabel(selectedDate.shortDayName(i));
    hWeek->addWidget(weekLbl[i],1,i);
    }

    QTextBrowser *txtDate=new QTextBrowser;

    for(i=0;i<7;i++)
    {
    for(j=0;j<7;j++)
    {
    if(count<=42)
    {
    button[i][j] = new QPushButton(QString::number(count));
    controlsLayout->addWidget(button[i][j], i, j);
    count++;
    }
    }
    }

    controlsLayout->setMargin(0);
    controlsLayout->setHorizontalSpacing(0);
    controlsLayout->setVerticalSpacing(0);
    h->addWidget(backwardBut);
    h->addWidget(blank);
    h->addWidget(MonthLbl);
    h->addWidget(blank);
    h->addWidget(forwardBut);
    hLbl->addWidget(blankLbl);
    v->addLayout(h);
    v->addLayout(hLbl);
    v->addLayout(hWeek);
    v->addLayout(controlsLayout);
    v->addWidget(txtDate);
    //centralWidget->setLayout(controlsLayout);
    centralWidget->setLayout(v);
    setCentralWidget(centralWidget);
    connect(forwardBut,SIGNAL(clicked()),this,SLOT(monthForward()));
    connect(backwardBut,SIGNAL(clicked()),this,SLOT(monthBackward()));
    }
    void MainWindow::monthForward()
    {
    MonthLbl->setText(selectedDate.longMonthName(++x));
    if(x>12)
    x=1;
    }
    void MainWindow::monthBackward()
    {
    MonthLbl->setText(selectedDate.longMonthName(--x));
    if(x<2)
    x=13;
    }
    with regards
    Anshuman

  15. #15
    Super Contributor
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    9,968

    Re: Creating an Array of Button

    HI have you checked the #9 link which has a complete datepicker code . why not you use the same then creating your own.

Similar Threads

  1. Replies: 6
    Last Post: 2011-01-19, 18:44
  2. Reg:creating an array
    By kathir.s in forum Symbian
    Replies: 6
    Last Post: 2010-07-19, 12:24
  3. Difference between array of pointes and pointer to an array
    By Pradeep Chandra in forum Open C/C++
    Replies: 2
    Last Post: 2008-09-15, 13:45
  4. Problems with creating an array with variable length
    By kaiten-sushi in forum Symbian
    Replies: 10
    Last Post: 2007-02-01, 11:27
  5. Replies: 2
    Last Post: 2005-03-14, 15:22

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×