Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How To Set BackgroundImage in Button By using codding
QtWS25 Last Chance

How To Set BackgroundImage in Button By using codding

Scheduled Pinned Locked Moved Mobile and Embedded
12 Posts 5 Posters 8.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BorahAnshuman
    wrote on last edited by
    #1

    hello Friends,

    I want to set an backgroundImage in my button by using a code rather by setting the property present in Qt Designer..so if anyone knows plz suggest me..

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      What sort of button? QPushButton? If so then you can use a custom stylesheet to do this. Something like:

      @
      QPushButton {
      background: myBackgroundImage.png;
      }
      @

      If you are referring to a QML button then use an Image element with an appropriate source property.

      Edit: see the "docs":http://doc.qt.nokia.com/latest/stylesheet.html for more info and examples on stylesheets

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DenisKormalev
        wrote on last edited by
        #3

        As he said about Qt Designer I think he is referring to QPushButton. So such stylesheet should work.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BorahAnshuman
          wrote on last edited by
          #4

          hello ZapB

          @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

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on last edited by
            #5

            Oh no we're not back on this calendar thing are we? I thought we had already told you how to do this by sub-classing QCalendarWidget and overriding the virtual paintCell() function.

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BorahAnshuman
              wrote on last edited by
              #6

              how can i setTextcolor to QpushButton without i hv use this two way but it wont work..is their another way for that...

              @QPalette pal = button[i][j]->palette();
              pal.setColor(QColorGroup::ButtonText, QColor(255, 0, 0));
              pal.setColor(QColorGroup::Button, QColor(255, 255, 0));
              button[i][j]->setPalette(pal);@

              and Second i have used styleSheet....

              @QString style="QMenu { font-size:16px; width: 150px; left: 20px;"
              "background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #cccccc, stop: 1 #333333);}";

              button[i][j]->setStyleSheet(style);@

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                ZapB
                wrote on last edited by
                #7

                I'm not going to get into another pointless discussion where you do not even attempt to take on board what others are telling you. In the above example your stylesheet selector applies to QMenu so why on earth do you think it will have an effect on a QPushButton. Please think about what you are doing before asking us at every single baby step. Also, please write in coherent sentences.

                Nokia Certified Qt Specialist
                Interested in hearing about Qt related work

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  Hi Borah,

                  it works, as you see in this screen shot:

                  !https://lh4.googleusercontent.com/_m1PNLlZctqY/TbW6bbPNakI/AAAAAAAAAF8/PtxuPJlkAvE/s800/button.jpg(green button)!

                  the used code is this:

                  @
                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);

                  QPushButton button;
                  QPalette pal = button.palette();
                  pal.setColor(QPalette::ButtonText, Qt::red);
                  pal.setColor(QPalette::Button, Qt::green);
                  
                  pal.setColor(QPalette::Light, Qt::yellow);
                  pal.setColor(QPalette::Midlight, Qt::darkYellow);
                  pal.setColor(QPalette::Shadow, Qt::magenta);
                  pal.setColor(QPalette::Dark, Qt::darkMagenta);
                  button.setPalette(pal);
                  button.setText("Hello");
                  
                  a.setStyle(new QWindowsStyle());
                  
                  button.show();
                  return a.exec&#40;&#41;;
                  

                  }
                  @

                  As you see in my code, I set the style to use. This is, because the win7 style uses different methods for trawing and ignors the background and 3D colors.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BorahAnshuman
                    wrote on last edited by
                    #9

                    Thanks Gerolf....its working nw..

                    Gerolf actually 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 rows and 7 coloums…
                    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…

                    with regards
                    Anshuman

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DenisKormalev
                      wrote on last edited by
                      #10

                      Are you sure your question is related to original topic question? I think it will be better to create a new thread with new question.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        Hi BorahAnshuman,

                        I will not write your code, you asked how to style the button, you got the answer. I will not think about how to implement your calendar based on buttons.

                        see Zap's post:

                        [quote author="ZapB" date="1303727762"]Oh no we're not back on this calendar thing are we? I thought we had already told you how to do this by sub-classing QCalendarWidget and overriding the virtual paintCell() function.[/quote]

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          leon.anavi
                          wrote on last edited by
                          #12

                          [quote author="BorahAnshuman" date="1303797884"]
                          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…[/quote]

                          Hi Anshuman,

                          Just a hint - handle signals and use "setText ( const QString & text )":http://doc.qt.nokia.com/4.7/qabstractbutton.html#text-prop to update each button. But as Gerolf already said you should not expect anyone to write your application.

                          Best regards,
                          Leon

                          http://anavi.org/

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved