久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

獲取二維數(shù)組 QPushButton 上 QPushButton 的索引

Get index of QPushButton on 2D array QPushButton(獲取二維數(shù)組 QPushButton 上 QPushButton 的索引)
本文介紹了獲取二維數(shù)組 QPushButton 上 QPushButton 的索引的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個二維數(shù)組 QPushButton,當用戶點擊按鈕時,如何獲取按鈕的索引?例如當用戶點擊按鈕 a[2][3] 它會顯示 (2,3) ?

I have an 2D array QPushButton, how can I get index of the button when user clicks on its? such as When user clicks on the button a[2][3] it will show (2,3) ?

推薦答案

示例如下:

您可以為按鈕指定唯一的對象名稱.理想情況下,名稱應該是有效的 C++ 標識符.

You can give your buttons unique object names. The names should ideally be valid C++ identifiers.

// https://github.com/KubaO/stackoverflown/tree/master/questions/button-grid-22641306
#include <QtGui>
#if QT_VERSION_MAJOR >= 5
#include <QtWidgets>
#endif

struct Display : QLabel {
   Q_SLOT void onClicked() {
      auto const elements = sender()->objectName().split('_');
      auto const i = elements.at(1).toInt();
      auto const j = elements.at(2).toInt();
      setText(QString{"(%1,%2)"}.arg(i).arg(j));
   }
   Q_OBJECT
};

int main(int argc, char *argv[])
{
   QApplication a{argc, argv};
   QWidget window;
   QGridLayout layout{&window};
   QVarLengthArray<QPushButton, 12> buttons(12);
   Display display;

   const int rows = 4, columns = 3;
   for (int i = 0; i < rows; ++ i)
      for (int j = 0; j < columns; ++j) {
         auto & button = buttons[i*columns+j];
         button.setText(QString{"(%1,%2)"}.arg(i).arg(j));
         button.setObjectName(QString{"buton_%1_%2"}.arg(i).arg(j));
         layout.addWidget(&button, i, j);
         display.connect(&button, SIGNAL(clicked()), SLOT(onClicked()));
      }
   layout.addWidget(&display, rows, 0, 1, columns);

   window.show();
   return a.exec();
}
#include "main.moc"

Qt 5 - 使用 Lambda

在 Qt 5 和 C++11 中,您應該使用函子為每個按鈕即時生成自定義槽.例如:

Qt 5 - Using Lambdas

In Qt 5 and C++11, you should use functors to generate custom slot for each button, on the fly. For example:

// https://github.com/KubaO/stackoverflown/tree/master/questions/button-grid-22641306
#include <QtWidgets>

int main(int argc, char *argv[])
{
   QApplication a{argc, argv};
   QWidget window;
   QGridLayout layout{&window};
   QVarLengthArray<QPushButton, 12> buttons(12);
   QLabel display;

   const int rows = 4, columns = 3;
   for (int i = 0; i < rows; ++ i)
      for (int j = 0; j < columns; ++j) {
         auto text = QStringLiteral("(%1,%2)").arg(i).arg(j);
         auto & button = buttons[i*columns+j];
         button.setText(text);
         layout.addWidget(&button, i, j);
         QObject::connect(&button, &QPushButton::clicked, [&display, text] {
            display.setText(text);
         });
      }
   layout.addWidget(&display, rows, 0, 1, columns);

   window.show();
   return a.exec();
}

Qt 4/5 - 使用 QSignalMapper

QSignalMapper 非常適合您的需求.它允許您將 QObject* 映射到其他東西",例如字符串.例如:

Qt 4/5 - Using QSignalMapper

QSignalMapper is pretty much designed for what you want. It lets you map a QObject* to "something else", like a string. For example:

#include <QtGui>
#if QT_VERSION_MAJOR >= 5
#include <QtWidgets>
#endif

int main(int argc, char *argv[])
{
   QApplication a{argc, argv};
   QSignalMapper mapper;
   QWidget window;
   QGridLayout layout{&window};
   QVarLengthArray<QPushButton, 12> buttons(12);
   QLabel display;

   const int rows = 4, columns = 3;
   for (int i = 0; i < rows; ++ i)
      for (int j = 0; j < columns; ++j) {
         auto text = QString{"(%1,%2)"}.arg(i).arg(j);
         auto & button = buttons[i*columns+j];
         button.setText(text);
         layout.addWidget(&button, i, j);
         mapper.connect(&button, SIGNAL(clicked()), SLOT(map()));
         mapper.setMapping(&button, text);
      }
   display.connect(&mapper, SIGNAL(mapped(QString)), SLOT(setText(QString)));
   layout.addWidget(&display, rows, 0, 1, columns);

   window.show();
   return a.exec();
}

Qt 4/5 - 使用屬性系統(tǒng)

您可以利用 QWidgetQObject 的事實.QObjects 有一個屬性系統(tǒng),因此您可以將每個按鈕的索引設置為一個屬性,然后在連接到 clicked() 信號的插槽中檢索它.例如:

Qt 4/5 - Using the Property System

You can leverage the fact that a QWidget is a QObject. QObjects have a property system, so you can set each button's index as a property, and then retrieve it in the slot connected to the clicked() signal. For example:

#include <QtGui>
#if QT_VERSION_MAJOR >= 5
#include <QtWidgets>
#endif

const char kIndex[] = "index";
struct Display : QLabel {
   Q_SLOT void onClicked() {
      setText(sender()->property(kIndex).toString());
   }
   Q_OBJECT
};

int main(int argc, char *argv[])
{
   QApplication a{argc, argv};
   QWidget window;
   QGridLayout layout{&window};
   QVarLengthArray<QPushButton, 12> buttons(12);
   Display display;

   const int rows = 4, columns = 3;
   for (int i = 0; i < rows; ++ i)
      for (int j = 0; j < columns; ++j) {
         auto index = QString{"(%1,%2)"}.arg(i).arg(j);
         auto & button = buttons[i*columns+j];
         button.setText(index);
         button.setProperty(kIndex, index);
         layout.addWidget(&button, i, j);
         display.connect(&button, SIGNAL(clicked()), SLOT(onClicked()));
      }
   layout.addWidget(&display, rows, 0, 1, columns);

   window.show();
   return a.exec();
}
#include "main.moc"

這篇關于獲取二維數(shù)組 QPushButton 上 QPushButton 的索引的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 成人午夜精品 | 国产久 | 日韩在线小视频 | 欧美a在线 | 欧美性久久 | 超碰男人天堂 | 中文字幕国产在线 | 国产精品日韩欧美一区二区三区 | 国产一级片在线观看视频 | 日韩国产在线观看 | 国产免费拔擦拔擦8x高清 | 日韩av成人| 欧美一级在线 | 日韩欧美在线观看视频网站 | 亚洲一区二区中文字幕在线观看 | 久久久青草婷婷精品综合日韩 | 青青草av网站| 久草视频在线播放 | 日韩免费网 | 欧美日韩久| 精品一区二区三区四区在线 | 99亚洲精品视频 | 中文字幕av网站 | 国产视频中文字幕 | 国产国产精品久久久久 | 精品中文字幕久久 | 一级做a爰片性色毛片视频停止 | 国产欧美日韩精品在线观看 | 伊人二区 | 精品综合久久久 | 亚洲国产精品久久久久婷婷老年 | 伊人国产精品 | 国产午夜精品一区二区三区四区 | 免费毛片网站 | 孕妇一级毛片 | 久久高清免费视频 | 高清一区二区 | 欧美日韩在线一区二区三区 | 成人特区 | 日韩另类 | 久久久综合精品 |