GMT 시간을 10 Hz로 표시해야하는 간단한 대화 위젯에서 작업 해 왔습니다. 며칠이나 며칠 동안 실행중인 시스템이 안정적이어야합니다.gettimeofday, QTime 또는 QLabel-> setText에서 CPU 사용률 100 %
몇 시간 동안 실행하면 몇 시간 동안 실행 한 후에 "BAR"프로그램이 100 % 실행되고있는 것으로 나타났습니다. 나는이 문제가 발생 이유에 어리둥절 해요,하지만 난 세 가지 기능으로 범위를 좁힐 수있었습니다 :
나는 하루 중 시간을 얻기 위해 ace_time
라는 간단한 함수를 사용하고 있습니다 :
inline double ace_time(void)
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (double) tv.tv_sec + 1e-6 * (double) tv.tv_usec;
}
을 그리고 나서이 함수가 반환 될 때 밀리 초, 초, 분 등을 얻습니다. 나는 다음의 형식을 QTime
를 사용
QTime time(hours, minutes, seconds, milliseconds);
QString timeStr = time.toString("hh:mm:ss.zzz");
을 그리고 내 레이블의 텍스트를 설정합니다
clock->setText(timeStr);
내가 100 %의 CPU 사용률을 얻을 왜 내가 혼란 스러워요을 gettimeofday
, QTime
또는 setText
하지 않는 한 내가 기대하지 않는 일을하고있다.
여기의 전문가가 이상한 행동을하는 기능을 발견 했습니까?
도움이된다면 Qt 4.8을 사용하고 있습니다.
이 문제를 해결하기위한 몇 가지 아이디어가 기대됩니다. 감사!
더 많은 코드를 추가 :
나는 두 개의 막대를 갖고 싶어. 위쪽 및 아래쪽 막대. 그래서 나는 BarBase
클래스와 TopBar
클래스를 작성했습니다. 레이아웃을 도와주기 위해 맞춤 QLayout
도 작성해야했습니다. 막대가 크기가 조정되고 형상을 다시 계산해야 할 때만 호출되기 때문에 레이아웃 관리자가이 문제를 일으키는 지 의심 스럽습니다.
class BarBase : public QWidget
{
public:
BarBase()
{
setFixedHeight(barHeight);
setContentsMargins(5, 0, 5, 0);
QPalette palette;
QColor color(50, 252, 50);
palette.setColor(QPalette::Background, color);
setAutoFillBackground(true);
setPalette(palette);
}
virtual ~BarBase();
protected:
QLabel *createWestLabel(const QString &);
QLabel *createCenterLabel(const QString &);
QLabel *createEastLabel(const QString &);
private:
QLabel *createLabel(const QString &, Qt::Alignment)
{
QLabel *label = new QLabel(str);
label->setAlignment(Qt::AlignVCenter | alignment);
//label->setFrameStyle(QFrame::Box | QFrame::Raised);
QFont font("Times");
font.setPixelSize(barHeight - 4);
font.setBold(true);
label->setFont(font);
return label;
}
};
그리고 여기에는 TopBar
내 클래스 만
class TopBar : public BarBase
{
Q_OBJECT
public:
TopBar()
{
Layout *layout = new Layout;
classification = createCenterLabel("Classification");
layout->addWidget(classification, Layout::Center);
hostname = createWestLabel("Hostname");
layout->addWidget(hostname, Layout::West);
layout->addWidget(createWestLabel(":"), Layout::West);
software = createWestLabel("Software");
layout->addWidget(software, Layout::West);
runMode = createEastLabel("SIM");
layout->addWidget(runMode, Layout::East);
layout->addWidget(createEastLabel(":"), Layout::East);
clock = createClockLabel("-dd::hh::mm::ss.z");
layout->addWidget(clock, Layout::East);
deadman = new QTimer;
connect(deadman, SIGNAL(timeout()), this, SLOT(updateLocalGMT()));
deadman->start(100); // 10 ms;
setLayout(layout);
setWindowTitle(tr("Top Bar"));
}
virtual ~TopBar();
public slots:
void updateLocalGMT()
{
double milliseconds = fmod(ace_time(), 86400.0) * 1000;
bool sign = (milliseconds >= 0.0);
if (!sign)
{
milliseconds = -milliseconds;
}
const int millisecondsToDays = 86400.0 * 1000.0;
const int millisecondsToHours = 3600.0 * 1000.0;
const int millisecondsToMinutes = 60 * 1000.0;
const int millisecondsToSeconds = 1000.0;
double days = floor(milliseconds/millisecondsToDays);
milliseconds -= days * millisecondsToDays;
double hours = floor(milliseconds/millisecondsToHours);
milliseconds -= hours * millisecondsToHours;
double minutes = floor(milliseconds/millisecondsToMinutes);
milliseconds -= minutes * millisecondsToMinutes;
double seconds = floor(milliseconds/millisecondsToSeconds);
milliseconds -= seconds * millisecondsToSeconds;
QTime time(hours, minutes, seconds, milliseconds);
/*
if (!time.isValid())
{
INFO("Invalid input to QTime [day, hour, min, sec, ms]: [%f %f %f %f %f]",
days, hours, minutes, seconds, milliseconds);
}
*/
QString timeStr = time.toString("hh:mm:ss.zzz");
timeStr = timeStr.left(timeStr.length() - 2); // to remove the last two z
timeStr.prepend((sign) ? "+" : "-");
timeStr.prepend("<code style='color:white'>");
timeStr.append("</code>");
// timeStr = timeStr.left(timeStr.length() - 2);
// qDebug() << currentTime;
clock->setText(timeStr);
}
private:
QLabel *classification;
QLabel *hostname;
QLabel *software;
QLabel *runMode;
QLabel *clock;
QLabel *createClockLabel(const QString &text)
{
QLabel *label = new QLabel(text);
label->setAlignment(Qt::AlignVCenter);
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
font.setFixedPitch(true); // enforces monospace
font.setPointSize(18);
font.setBold(true);
label->setFont(font);
int pixelWidth = label->fontMetrics().width(label->text());
label->setFixedWidth(pixelWidth);
return label;
}
QTimer *deadman;
};
루프에서이 작업을 수행하고 있습니까? –
나는 그렇게 생각하지 않는다. 나는 100ms의 타임 아웃을하는'qTimer '를 가지고 있으며,이 함수를 사용하는 슬롯에 신호를 연결했다. –
이러한 함수 중 어느 것도 0.1을 사용하지는 않을 것 같습니다. 나는 당신이 게시하지 않은 코드 어딘가에 버그가 있다고 의심한다 ... –